How can I use Autolayout to set constraints on my UIScrollview?

后端 未结 18 1598
别那么骄傲
别那么骄傲 2020-11-28 00:43

I have spent two days trying out the various solutions for Mixed and Pure Autolayout approaches to achieve what was a trivial scrollview setup prior to autolayout, and it\'s

相关标签:
18条回答
  • 2020-11-28 01:31

    There is a piece in the tech notes that you may have looked over. You can implicitly set the content size of a scroll view using constraints fixed to the edges of the scroll view.

    Here's a simple example. Create a storyboard with one view, that has one scroll view. Set that scroll views constraints to make it fit the size of the view you put it in.

    Inside that scroll view add a single view. Explicitly set the size of that view using constraints (and make sure that size is bigger than the scroll view).

    Now add four more constraints to that inner view locking the four edges of the inner view to its parent scroll view. Those four constraints will cause the content size to expand to accommodate the inner view.

    If you have multiple views you want to add to a scroll view, for example laid out horizontally, you'd lock the left side of the first subview to the left of the scroll view, lock the subviews to each other horizontally, and the right side of the last sub view to the right side of the scroll view. Those constraints would force the content size of the scroll view to expand to accommodate all of the subviews and their constraints.

    0 讨论(0)
  • 2020-11-28 01:33

    There are so many questions about using AutoLayout with UIScrollView, the key point which we ignore is that the inner views of the UIScrollView make constraints against the Content View but not the UIScrollView itself. Refer to the Technical Note TN2154, you can find:

    The UIScrollView class scrolls its content by changing the origin of its bounds. To make this work with Auto Layout, the top, left, bottom, and right edges within a scroll view now mean the edges of its content view.

    The following figure will depicts that: enter image description here

    You can find the trailing space is 500 points, if the constraint is made to the UIScrollView, the view will be miss placed and should be update its frame. However, no warnings and no errors. Because all the constraints are against the content view.

    UIScrollView will calculate the size of the content view according to the constraints of the inner views. (For the example, the content size: width = 100(leading space) + 200 (view's width) + 500 (trailing space), height = 131 (top spacing) + 200(height) + 269(bottom spacing)

    How to add constraints for views in the UIScrollView:

    1. Imaging the positions of views in the content view.
    2. Add top, right, bottom, left spacing to the edges of the content view, in addition, also the width and height of these views.

    And all it is done.

    An easy way to deal with AutoLayout with scrollview is to add a container view containing all subviews in the scroll view.

    Conclusion: the key point to understand AutoLayout with UIScrollView is inner views make constraints against the content view but not UIScrollView itself.

    attached example code

    0 讨论(0)
  • 2020-11-28 01:34

    After some time dealing with this issue, I finally found a solution. I'm working with universal class sizes storyboards (600x600). I created a UIView (contentView) the size of the scrollView and created constraints to Top, Bottom, Leading and Trailing to the scrollView. Then I clipped the size manually of the contentView to 600x600. The storyboard stopped trying to resize everything and I could work but the view looked awful on the real device or simulator. I made 2 constraint outlets of this clipped sizes.

    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentViewWidthConstraint; 
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentViewHeightConstraint;
    

    Then in viewDidLoad

    CGSize viewSize = self.view.frame.size;
    self.contentViewWidthConstraint.constant = viewSize.width;
    self.contentViewHeightConstraint.constant = viewSize.height;
    

    Works great.

    0 讨论(0)
  • 2020-11-28 01:35

    If your question is "How do I put a bunch of UITextFields in a vertically scrolling UIScrollView such that they move out of the way of the keyboard when they have focus", the best answer is:

    Don't.

    Use a UITableViewController with static cells instead.

    You get this scroll-out-of-the-way behaviour for free, AND all the content insets Just Work if your view controller is displayed inside a UINavigationController.

    0 讨论(0)
  • 2020-11-28 01:36

    I know this is a layman's solution and not what Apple suggests in the docu, but it worked for me twice, with different content and can be set up very quickly: In the storyboard view controller insert UIView. In UIView insert a Table View, Dynamic, 0 Prototype cells, Style Plain or Grouped. In Table View insert a Scroll View, in Scroll View insert content. Thats it, no settings in the custom view controller.

    0 讨论(0)
  • 2020-11-28 01:40

    I spent days trying to find a solution of how to use AutoLayout view an embedded Scrollview, to centre the scrollview in the visible screen, that works across all devices / screen dimensions as well as with screen rotation.

    I spent days trying to do it with Autolayout only, and got close but never close enough. So in the end I had to add 3 lines of code per screen as well, in viewDidLoad.

    See solution below :

    1. Create the scrollview and fill it with whatever objects you want
    2. Turn on auto layout
    3. Then Centre the ScrollView Vertically and Horizontally Centre ScrollView
    4. Select the View and then 'Add missing constraints' - this then does its thing
    5. The result is that the a lot of constraints are generated. There are 2 new ones created for the view : 'Horiz space scrollview to View' and 'Vert space scrollview to view' or vice-versa.
    6. Delete the 'Horiz space scrollview to View' so you are now left with 3 constraints on the View. The 2 for entering the scrollview in the view and the one to set a vertical space between the scrollview and the view
    7. Now link the Vert constraint to your code by click and Ctrl dragging it to the header file and creating an NSLayoutConstraint IBOutlet (I called mine constraintVertVtoSV)
    8. Now go to the .m file and add these lines of code into viewDidLoad (play with the padding amount to get the correct vert centering)

      if (IPAD)
      

      { self.constraintVertVtoSV.constant = 150.0; }

    9. this should now run on all devices and be properly centered and still scroll properly.

    0 讨论(0)
提交回复
热议问题