I am trying to work an example code thats using the 'mixed approach' mention on apple dev link:
I am trying to stack 3 views vertically in UIScrollView. In below example the UIScrollView shows the red view on load but does not scroll as expected. I can scroll a little bit to see the green view below the red view - but the scroll view springs back up and does not scroll to the green view or view below it(blue view). I understand I need a constraint I tried to add one between view 1 & 2 so that view2.top = view1.bottom ,but seems I am missing something.
Also I noticed the content size of the scrollview is zero ( in viewDidAppear method).
Any tips on what I am missing or help on how to get this mixed approach working would be of great!!
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIScrollView* scrollView = ((UIScrollView*)self.view); scrollView.translatesAutoresizingMaskIntoConstraints = NO; CGFloat w = self.view.frame.size.width; CGFloat h = self.view.frame.size.height; contentView = [[UIView alloc] initWithFrame:CGRectMake(0,0, w, h*3)]; [scrollView addSubview:contentView]; v1 =[[UIView alloc] initWithFrame:CGRectMake(0,0, w, h)]; v2 =[[UIView alloc] initWithFrame:CGRectMake(0,h, w, h)]; v3 =[[UIView alloc] initWithFrame:CGRectMake(0,h*2, w, h)]; v1.backgroundColor = [UIColor redColor]; v2.backgroundColor = [UIColor greenColor]; v3.backgroundColor = [UIColor blueColor]; [contentView addSubview:v1]; [contentView addSubview:v2]; [contentView addSubview:v3]; NSLayoutConstraint *myConstraint =[NSLayoutConstraint constraintWithItem:v1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:v2 attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]; [contentView addConstraint:myConstraint]; scrollView.contentSize = contentView.bounds.size; }