3/28/2019 - 5:47 AM

Example of setting up a UIScrollView programmatically in Objective-C, using Auto Layout. We put a UIView inside the scroll view as its conte

Example of setting up a UIScrollView programmatically in Objective-C, using Auto Layout. We put a UIView inside the scroll view as its content view, and add subviews to the content view. The scroll view scrolls vertically. (See Apple Technical Note: TN2154.)

- (void)viewDidLoad
    [super viewDidLoad];
    // See TN2154:
    UIScrollView* scrollView = [[UIScrollView alloc] init];
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    [[self view] addSubview:scrollView];
    // Pin the scroll view to the main view.
    [[scrollView topAnchor] constraintEqualToAnchor:[[[self view] safeAreaLayoutGuide] topAnchor]].active = YES;
    [[scrollView leadingAnchor] constraintEqualToAnchor:[[self view] leadingAnchor]].active = YES;
    [[scrollView trailingAnchor] constraintEqualToAnchor:[[self view] trailingAnchor]].active = YES;
    [[scrollView bottomAnchor] constraintEqualToAnchor:[[self view] bottomAnchor]].active = YES;
    UIView* contentView = [[UIView alloc] init];
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [scrollView addSubview:contentView];
    // Pin the content view's top and bottom to the scroll view, and handle width and height separately.
    [[contentView topAnchor] constraintEqualToAnchor:[scrollView topAnchor]].active = YES;
    [[contentView bottomAnchor] constraintEqualToAnchor:[scrollView bottomAnchor]].active = YES;
    // The content view must explicitly communicate its width and height to the scroll view.
    [[contentView widthAnchor] constraintEqualToAnchor:[scrollView widthAnchor]].active = YES;
    // The height is communicated by an unbroken chain of constraints, from top to bottom, on the subviews.
    NSArray* rainbow = @[[UIColor redColor],
                         [UIColor orangeColor],
                         [UIColor yellowColor],
                         [UIColor greenColor],
                         [UIColor blueColor],
                         /* indigo */
                         [[UIColor alloc] initWithRed:75/255.0 green:0.0 blue:130/255.0 alpha:1.0],
                         /* violet */
                         [[UIColor alloc] initWithRed:111/255.0 green:74/255.0 blue:207/255.0 alpha:1.0]];
    NSLayoutYAxisAnchor* bottomAnchor = [contentView topAnchor];
    UIView* aView = nil;
    for (UIColor* color in rainbow) {
        aView = [[UIView alloc] init];
        aView.translatesAutoresizingMaskIntoConstraints = NO;
        [contentView addSubview:aView];
        [[aView topAnchor] constraintEqualToAnchor:bottomAnchor].active = YES;
        [[aView leadingAnchor] constraintEqualToAnchor:[contentView leadingAnchor]].active = YES;
        [[aView trailingAnchor] constraintEqualToAnchor:[contentView trailingAnchor]].active = YES;
        [[aView heightAnchor] constraintEqualToConstant:125.0].active = YES;
        [aView setNeedsUpdateConstraints];
        bottomAnchor = [aView bottomAnchor];
        aView.backgroundColor = color;
    [[aView bottomAnchor] constraintEqualToAnchor:[contentView bottomAnchor]].active = YES;
    [contentView setNeedsUpdateConstraints];
    [scrollView setNeedsUpdateConstraints];
    [[self view] setNeedsUpdateConstraints];