简体   繁体   中英

iOS 7 - UIScrollView subView not showing

Trying to add subviews to UIScrollView, which is defined in XIB file. The code below is how I tried to add subviews in my view controller. But no subviews were added.

Any suggestions?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    CGRect frame = _tasksScrollView.frame;
    frame.size.width /= 3.0;

    NSArray *colors = [NSArray arrayWithObjects:[UIColor blueColor],
                                                [UIColor redColor],
                                                [UIColor yellowColor],
                                                [UIColor greenColor],
                                                [UIColor purpleColor],
                                                nil];

    for (int i = 0; i < [colors count]; i++, frame.origin.x += frame.size.width) {
        UIView *subView = [[UIView alloc] initWithFrame:frame];
        subView.backgroundColor = [colors objectAtIndex:i];

        [_tasksScrollView addSubview:subView];
    }

    _tasksScrollView.contentSize = CGSizeMake(frame.size.width * [colors count], frame.size.height);
}

It may be that your subviews are offset relative to the view space in your scroll view since youre carrying over the x and y coordinates from the scrollview.frame itself which is actually a position relative to the full screen. Try setting the x and y position of the subview so that it starts from 0 .

造成这种情况发生的原因有很多,在大多数情况下,您只是无法链接/连接XIB中的IBOutlet属性(_tasksScrollView)。

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