0

I have set following contrstraints to my ViewController

enter image description here

Then I add custom views like this.

-(void)setUI
  {
    int i;
for ( i=0; i<[array count]; i++)
 {

    NSLog(@"Object at index %i  %@",i,[array objectAtIndex:i]);

    UIView *view=[self GetAlertView:i];


    float sizeOfContent = 0;

    if (notY+view.frame.size.height>self.contentView.frame.size.height+self.contentView.frame.origin.y) {
        CGRect frame=self.contentView.frame;
        frame.size.height=notY;
        self.contentView.frame=frame;
    }


    [self.contentView addSubview:view];
    self.mainScroll.contentSize = self.contentView.frame.size;

    NSLog(@"CONTENT VIEW FRAME HEIGHT==== %f",self.contentView.frame.origin.y+self.contentView.frame.size.height);

   }


 }

But my problem is this content view not increase its size permanently. In log cat I can see hight is changing.

CONTENT VIEW FRAME HEIGHT==== 8913.000000

But it not persistence. In the UI I can see its hight just remains a certain height and only 3 sub views have added into the content view and other views are out of the Content view. What is the reason for this? Please help me Thanks

user1960169
  • 3,533
  • 12
  • 39
  • 61
  • I think take this line out of for loop self.mainScroll.contentSize = total height and change it to this mentioned line and call [self.view layoutsubviews] method after the for loop. – Muhammad Waqas Bhati Nov 17 '15 at 05:10

1 Answers1

1

When you are using AutoLayout, you should avoid setting the contentSize of UIScrollView and instead depend on the subview's height and bottom constraint to let the UIScrollView calculate it internally.

Second, you are using an unwanted UIView to render your subviews.You don't need ContentView to host your subviews. Directly add those to mainScroll view.

Use this code to layout subviews vertically in your scroll view. Here i have taken some assumptions such as view.width = mainScroll.width and view.width = view.height and view is having a padding of 5px to each boundry. You can make changes as you like.
First, create an instance variable in your class as:

UIView *lastView;  
NSInteger arrayCount;

Then replace your setUI code with this

-(void)setUI
{
    lastView = nil;
    arrayCount = [array count];

    for(NSInteger index =0; index < arrayCount; index++)
    {
        UIView *view = [self GetAlertView:i];
        [self.mainScroll addSubview:view];

        [view setTranslatesAutoresizingMaskIntoConstraints:NO];

        [self.mainScroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[view]-0-|" options:0 metrics:nil views:@{@"view":view}]];

        //--> If View is first then pin the top to main ScrollView otherwise to the last View.
        if(lastView == nil && index == 0) {
            [self.mainScroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[view]" options:0 metrics:nil views:@{@"view":view}]];
        }
        else {
            [self.mainScroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[lastView]-5-[view]" options:0 metrics:nil views:@{@"lastView":lastView, @"view":view}]];
        }

        [self.mainScroll addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.mainScroll attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
        [self.mainScroll addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];

        //--> If View is last then pin the bottom to mainScrollView bottom.
        if(index == arrayCount-1) {
            [self.mainScroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view]-5-|" options:0 metrics:nil views:@{@"view":view}]];
        }
        //--> Assign the current View as last view to keep the reference for next View.
        lastView = view;
    }
}
Gandalf
  • 2,399
  • 2
  • 15
  • 19
  • Thanks for the solution. I tried it but only first 2 views adding to the scroll view. And scrollview is not scrollable now. Why is that? – user1960169 Nov 17 '15 at 11:52
  • It's a working code as i am using it in my project. You removed the `ContentView` from storyboard, right? Do you see any conflicting constraints in debugger window? If yes post those too here. – Gandalf Nov 17 '15 at 12:00
  • yeas I removed that. And I cant see any conflicting constraints in debugger window. – user1960169 Nov 17 '15 at 12:06
  • I can scroll it horizontally. But not moving vertically and 1st 2 views I can see on the view and also the gap bitween those 2 views (vertical gap) seems more than 5. Its like around 100 – user1960169 Nov 17 '15 at 12:09
  • Hmmm, doesn't make much sense. I can help if you can share the code. – Gandalf Nov 17 '15 at 12:12
  • No, not in question. I need the whole working project code. If you can't share whole code then create a sample project and include the code of this scenario and share it with Github may be. – Gandalf Nov 17 '15 at 12:16
  • As per your suggestion, I removed unwanted contentview, but last time I had faced the same issue at that time it was solved only because of using that unwanted contentview which is mentioned in http://stackoverflow.com/a/27227174/2764966 answer . I don't know how it worked this time without using content view? – ViruMax Aug 10 '16 at 11:38
  • @ViruMax- The key here is how you can tell the scroll view to stretch it's contentView's size(in this case height) as per the combined heights of it's subviews. You can use that "unwanted contentview" if you want but now that contentview's height should be equal to sub of all subviews and again that contentview's height should be equal to the content size height of scroll view, which is the case mentioned in your linked answer. I hope it clarifies things. – Gandalf Aug 10 '16 at 12:01