开发者

A UIView subview doesn't show in UIScrollView

I'm a newbe on iOS development, I'm trying to make a scroll header like Engadget's app for iPhone, i created a custom UIView to act as a subview for UIScrollView, the algorithm for placing the subviews seems to be ok as I can Scroll the subviews, programatically i change the background color of the views, but the problem is that I can't see anything of the content, just gray backgrounds what am I doing wrong?

Thanks in advance.

This is my code:

Promoted is a UIView with two labels and a UIImageView.

- (void)layoutScrollSubViews
{
promoted *view = nil;
NSArray *subviews = [sView1 subviews];

// repo开发者_StackOverflowsition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
    if ([view isKindOfClass:[promoted class]]  && view.tag > 0)
    {
        CGRect frame = view.frame;
        frame.origin = CGPointMake(curXLoc, 0);
        frame.size.height = kScrollObjHeight;
        frame.size.width = kScrollObjWidth;
        view.frame = frame;

        curXLoc += (kScrollObjWidth);
    }
}

// set the content size so it can be scrollable
[sView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [sView1 bounds].size.height)];

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad{    
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{

    promoted *p = [[promoted alloc] initWithFrame:CGRectMake(0, 0, 320, 187)];

    p.title.text = @"Test 1";
    p.num.text = @"1/1";
    p.num.backgroundColor = [UIColor whiteColor];
    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
    p.backgroundColor = [UIColor colorWithRed:(.9/i) green:(.9/i) blue:(.9/i) alpha:1];

    p.tag = i;  // tag our images for later use when we place them in serial fashion

    [sView1 addSubview:p];
    [p release];
}

[self layoutScrollSubViews];
[super viewDidLoad];
}


You can do two things: 1. Add your image view and labels directly to scrollView 2. also alloc your label and ImageView before adding them to your UIView(and don't forgot to release them when requirement is finish, in respective scope)


As per my experience, UIScrollView is a weird buggy control released by apple. Don't know why it behaves abnormal when added through nib file. I always try to create UIScrollView programmatically and add subviews to it by allocating them programmatically as well.

May be it would sound strange, but it works for me most of the time!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜