简体   繁体   中英

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];

// reposition 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];
}

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!

您可以做两件事:1。将图像视图和标签直接添加到scrollView 2.还要在​​将它们添加到UIView之前scrollView您的标签和ImageView(并且在要求完成时不要忘记在相应的范围内发布它们)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM