2

I have a ViewController with a scrollView and a content View inside. Some labels and stuff inside the Content View as well.

ScrollView ScreenShot

Constraints ScreenShot

Now after that, when the view load i have to pass in data from other places to this view controller, and then load some other views by code.

The scrollview Content height and height did get updated, and i did place the loading and adding of the views at ViewWillAppear. But even if the content size is bigger than the height of the scrollview, i just can't scroll it.

Scroll View Height:667. Scroll View Content Height :1632 ContentView Height :1632

Cong Tran
  • 1,448
  • 14
  • 30
  • Have you added constraints to the views with respect to scrollview which are you adding programatically – Awesome.Apple Mar 11 '16 at 04:38
  • No, mainly i don't really know how to, will try again. Does it mean i have to add constraints to the labels that is in the view too? Or just add the constraints to the view with respect to the scroll view? But i added the views as subview into the Content View. Do i add the constraints to the ContentView? – Pan Kai Zhong Mar 11 '16 at 04:42
  • Please follow this 6 mins video https://www.youtube.com/watch?v=UnQsFlMGDsI and add your constraints accordingly. – Suresh Kumar Durairaj Mar 11 '16 at 04:46
  • Scroll view will not scroll, if it's height and content height are same. In order for scroll view to scroll the content, it's height should be less than a content height. – Fahri Azimov Mar 11 '16 at 04:51
  • I have already added the constraints necessary for the storyboard including all the views, labels accordingly. Its the part where i added in views by code need constraints. – Pan Kai Zhong Mar 11 '16 at 05:00

3 Answers3

2

Your scrollview did not scrolled because you gave fixed size to content view.

Don't worry, just make the outlet of height constraint.

Check below image , how to crete outlet of constrain,

enter image description here

Note :- You need to create out let of Height Constrain.

From your image.

enter image description here

And now set in your viewDidLayoutSubviews

-(void)viewDidLayoutSubviews
{
    NSInteger screenheight=[ [ UIScreen mainScreen ] bounds ].size.height;

    if (screenheight > 504 + 64)
    {
        _contentviewHeight.constant = screenheight-64;
    }

}

And last but not the least , Don't forget to remove Tick from Adjust Scrollview insects,

enter image description here

Badal Shah
  • 7,541
  • 2
  • 30
  • 65
  • where is this Adjust ScrollView insects? So far i followed your step up to changing the constraints.constant. And it doesn't work. – Pan Kai Zhong Mar 11 '16 at 06:05
  • Maybe because the current view controller storyboard is not the main storyboard, I can't find it. But i think i will be adding constraints by code to the view i added by code. Thanks for the effort anyway. – Pan Kai Zhong Mar 11 '16 at 06:29
  • what you didn't find ? and why you added constrain by code and storyboard both ? – Badal Shah Mar 11 '16 at 06:33
  • because The app looks good on Iphone 4 and 5 but on 6 its really ugly. So i have to use auto-layout. As for why add in view by code, well, because the individual view i want to add are really big and have lots of detail. I tried cramming everything into one view and it hang my Iphone,so add by code. – Pan Kai Zhong Mar 11 '16 at 06:59
  • ok no problem. It's depend on you. Actually when you set aspect ration then your view or any other control will resize automatically according to device. Last one is just a suggestion. – Badal Shah Mar 11 '16 at 07:02
0

In case of Autolayout, whatever view you are adding to scrollview. You have to add constraint (basically top constraint) so that Scrollview would calculate contentsize automatically.

refer below to add constraints programatically.

iOS create NSLayoutConstraint programmatically for table view cell content

Edit :-

Like you said SView is in storyboard. So create the constraint from storyboard itself on SView.

And E,R and H View will be added constraint programatically

And the link is just an example

You will use [self addConstraint] instead of [self.ContentView addConstraint]

Moreover refer this one too :-

iOS Autolayout Vertically equal space to fill parent view

Community
  • 1
  • 1
Awesome.Apple
  • 1,316
  • 1
  • 11
  • 24
  • So I added in 3 views by code, so total 4 views. Lets say, SView(the one in storyboard), EView, RView and HView. Is it suppose to be like this? [self.ContentView addConstraint:[NSLayoutConstraint constraintWithItem:EView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:SView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]]; And just keep adding downwards? Like SView-0-EView-0-RView-0-HView-0-| ? – Pan Kai Zhong Mar 11 '16 at 05:10
  • I added the constraints to view already, the scroll view can scroll but very little, barely showing EView. Do i add constraints to the height of each view? So far there are only 3 constraints that i added in by code. S View.bot = E View.Top; E View.bot = R View.Top; R View.bot = H View.top; Which other constraints will need to add? – Pan Kai Zhong Mar 11 '16 at 06:18
  • I have added in Height, Width and Leading Constraints to all view, but the scrollView still can't scroll all the way down. Is there something wrong? I put height as a fixed constant of 600 for all 3 views, and their width to be the same as contentView, their Left is also equal to contentView Left. Is the relation wrong or something else? It still scrolls the same small amount. – Pan Kai Zhong Mar 11 '16 at 06:51
0

OKAY, Finally.

After trying and retrying and to no avail. I just gave up on putting auto layout on the ViewController which have the scroll view itself.

So I untick the auto layout, and then VOILA.

Apparently, the view controller which have the scroll view don't need autolayout. Just the views that i put in via code need auto layout in their storyboard. After i adjust all the constraints in the view properly and never giving them aspect ratio constraints but just fixed their width and height. Done.

So I hope anybody who have the same frustration as me see this and heed my advice. Don't bother putting auto layout on the scroll view or its content View. Just Untick it altogether. Do the autolayout for the views you want to put in. You just have to specify the views' width and height properly and it will work.