开发者

can't add subview to UIScrollView

I am trying to create a horizontal scroll view which has a UILabel as elements, by doing the following in viewDidLoad:

for(int index=0; index < [self.category count]; index++)
    {
        UILabel *label = [[UILabel alloc] init];
        label.text = [self.category objectAtIndex:index];
        label.textColor = [UIColor blackColor];
        CGSize maximumLabelSize = CGSizeMake(100,9999);
        CGSize expectedLabelSize = [[self.category objectAtIndex:index] sizeWithFont:label.font                        
                                      constrainedToSize:maximumLabelSize 
                                          lineBreakMode:UILineBreakModeWordWrap]; 
        label.frame = CGRectMake(5+xOffset, 0, expectedLabelSize.width, 40);

        self.scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
        [self.scrollView addSubview:label开发者_开发知识库];

        xOffset += 170;
    }

However, I can't see anything when I run the app in the simulator, what am I missing here? Pretty sure that the UIScrollView is connected via the IBOutlet and I know that the text exists as I tried printing that out via NSLog

UPDATE: Also how do I check which UILabel is clicked? I wanted to know this as well..


I think you did'nt initialize xOffset=0 before running for loop. so that it is taking a garbage value and then executing xOffset += 170; instruction.

so please initialize xOffset=0;


If you're positioning the labels with a frame you should also set autoresizingMask to make sure if you position based on a portrait layout but are testing the app in landscape, your labels don't get auto-positioned outside the new bounds. Also, make sure to autorelease those labels or else you'll be leaking memory.


Your expectedLabelSize calculation seems to adjust the height, leaving the width at 100. When you set the frame of your UILabel you are just using this width.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜