dvdchr
dvdchr

Reputation: 734

What is the correct way to use UIScrollView?

I tried to setup a simple view which displays a text-only article with a headline. Here are the steps that I've done:

  1. Create View Controller with its .xib file,
  2. Create UIScrollView and places a UIView directly as the content wrapper,
  3. Set the constraints [scrollview]-0-[superview] in top, bottom, leading, and trailing.
  4. Set the constraints [content wrapper]-0-[scroll view] in top, bottom, leading, and trailing.
  5. Set Width and Height to content wrapper as placeholder.
  6. Add Label and UITextView as content wrapper's subviews.
  7. Add constraints to the subviews.
  8. Following this tutorial, I programmatically set content wrapper's leading = scrollview's superview left,
  9. ... And content wrapper's trailing = scrollview's superview right.

When I ran the code, it shows everything in place perfectly; The UIScrollView scrolls, margins are properly set, etc.

However, Xcode throws an exception:

2015-02-05 18:06:58.230 ScrollViewApp[5353:180401] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7ff9fa49a3f0 H:[UIView:0x7ff9fa571560(600)]>",
    "<NSLayoutConstraint:0x7ff9fa49b1a0 H:|-(0)-[UIView:0x7ff9fa571560]   (Names: '|':UIScrollView:0x7ff9fa49a910 )>",
    "<NSLayoutConstraint:0x7ff9fa49ce00 H:|-(0)-[UIScrollView:0x7ff9fa49a910]   (Names: '|':UIView:0x7ff9fa49a840 )>",
    "<NSLayoutConstraint:0x7ff9fa61c050 UIView:0x7ff9fa571560.right == UIView:0x7ff9fa49a840.trailing>",
    "<NSLayoutConstraint:0x7ff9fa580970 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7ff9fa49a840(375)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ff9fa49a3f0 H:[UIView:0x7ff9fa571560(600)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

After further googling, I found out that I can dismiss the warning by setting content wrapper's width (see step 5 above) priority to low; however it breaks the layout in interface builder. Take a look:

Content wrapper's width constraint set to low priority

Compared to when it's set to High priority:

Content wrapper's width constraint set to HIGH priority

I know that in the end it makes no difference because it's both working as expected.. But I'm kind of curious as to why these things happen..

I'm trying to understand how UIScrollView works, but maybe I'm misunderstanding something; so, what is the proper way for setting up UIScrollView to work as expected?

Upvotes: 2

Views: 327

Answers (2)

Shivani Gor
Shivani Gor

Reputation: 329

you can use scrollview like this. Add scrollview in design. Give it's left, top, right, bottom constraints in design. Add your subviews in scrollview. Add proper constraints for them. There is no need to add constraints in code. In viewDidLayoutSubviews set your scrollview's content size. - (void)viewDidLayoutSubviews {

[super viewDidLayoutSubviews];
CGRect boundsOfSelf = self.view.bounds;
scrollView.frame = boundsOfSelf;
[scrollView setContentSize: boundsOfSelf.size];

}

Upvotes: 0

Jad Feitrouni
Jad Feitrouni

Reputation: 637

Try to add a center horizontally constraint from your content wrapper to your scroll view. To be honest, I really don't know why that works, i figured this one out by trial an error. If you want your view to be compatible different screen sizes, remove the width constraint of the content wrapper. Hope it helped.

Upvotes: 1

Related Questions