2

Using the storyboard and auto-layout I drag a scroll view into the view controller and satisfy auto-layout constraints appropriately. I add in a Label centred on the screen with the appropriate auto-layout constraints.

Implemented the following code in viewDidLoad, viewWillAppear and viewDidLayoutSubviews (at different run times of course)

scrollView.contentSize.height = 1000

When running in the simulator the view only scrolls horizontally not vertically. It didn't make sense at first when seeing it scroll horizontally without applying specific conditions to do so, until i looked closely into the constraints.

When initially dragging in a UIScrollView and selecting the Pin option to add constraints it shows the leading and trailing space to have a default value of -20 instead of 0. This seems to explain the automatic horizontal scrolling but still does not explain why I am not able to implement vertical scrolling.

I have also tried specifying the content size using CGSize:

scrollView.contentSize = CGSize(400, 1000)

or

scrollView.contentSize = CGSize(view.frame.width, 1000)

neither seem to work. I also implemented the above methods with the same parameters using the initialiser CGSizeMake(width, height) and also does not work.

Scrolling is checked as enabled as are other options as per default when selecting a scroll view.

Vince
  • 691
  • 1
  • 8
  • 18
  • 1
    You should show us the constraints for your label. – beyowulf Dec 04 '15 at 02:37
  • at first read of your comment i thought the relevance of the labels constraints isn't important but got me thinking and i simply removed the label leaving only the scroll view and it now scrolls as intended. Can't believe it was the label holding the view back. Sorry for such a rookie mistake, appreciate the eye opener @beyowulf – Vince Dec 04 '15 at 02:58
  • @beyowulf my constraints were leading, trailing, bottom and top spacing to superview (Scroll View). Suggestions as to what the constraints should be to remove its control of preventing the view from scrolling vertically ? – Vince Dec 04 '15 at 03:06

1 Answers1

3

You can set the constraints to centered horizontally and vertically in container view. This has the unfortunate effect of raising an error "Scroll view has ambiguous content size." If you know for a fact you're going to set the content size in code, you can ignore it.

But it's probably a better practice to drag out an empty UIView call it containerView, pin it top/bottom/left/right to the scroll view center the label like above. Set a constraint for the containerView's height. Then create an IBOutlet for the height constraint of the containerView call it containerViewHeightConstraint. Then in code say:

self.containerViewHeightConstraint.constant = 1000 //or whatever height you want for scrolling 
beyowulf
  • 15,101
  • 2
  • 34
  • 40
  • I've seen your suggestion else where. could you clarify the steps for me. - drag scroll view into view controller. pin to top,bottom,left,right. - drag uiview into scroll view and pin it as above to scroll view - then drag in any wanted views into the uiview(2) and apply constraints appropriately - set the constraint for the uiview(containerView) height as the scrollable height i want to work with - the line of code you suggested I don't understand its purpose, i would then set instead the content size.height of the scroll view to the containerViews height – Vince Dec 04 '15 at 03:26
  • Oh, yeah. You can set it in IB or set it in code. It's up to you. If you know before hand, "I want the the scroll view content size to be exactly 1000" set it in IB. But if you want to compute it at runtime. Like, "I want it to scroll this labels height + some padding + this other labels height + some padding," You could use the line of code above to set it dynamically, where you replace 1000 with some calculation, in viewDidLayoutSubviews or after you fetched some data off a server, etc. – beyowulf Dec 04 '15 at 03:46
  • thats great thank you. just another small one while your active. I want to have a scroll view that displays say three labels positioned below each other from top of the view to say half way down. Then below these have a table view whose content will go past the content size hight due to the number of cells. then be able to scroll the screen vertically to reveal the hidden cell (but scroll on the scroll view not the table views scroll view). how should I go about that (if your able to answer) – Vince Dec 04 '15 at 03:58
  • I'm not sure I understand what you want. But adding a table view to a scroll view is generally a pain. If you want everything to scroll together it is better to create a tableview the entire view controller's view's height. Then layout a view on top of the table view with the labels the way you want them. Then in viewDidLoad say self.labelView.removeFromSuperView. Then in viewDidLayoutSubviews say self.tabelView.tableHeaderView = self.labelView this way everything will scroll together. – beyowulf Dec 04 '15 at 04:06
  • If you click this link it will show a visual of what i tried to explain. [Link to image](https://www.dropbox.com/s/6madpgvlgaz00ai/Screen%20Shot%202015-12-04%20at%203.24.03%20PM.png?dl=0) – Vince Dec 04 '15 at 04:26