Auto Layout iOS 7 - Can't align right of subview to right of UIScrollView

后端 未结 2 1945
耶瑟儿~
耶瑟儿~ 2021-02-14 11:31

I am programmatically building a view and using autolayout, no interface builder at all. In a custom ScrollView controller, I am adding a UILabel and a UIButton as subviews. I w

2条回答
  •  隐瞒了意图╮
    2021-02-14 12:20

    The number of constraints is not an issue. Both UILabel and UIButton will determine their size based on their intrinsicContentSize and since you have constraints for the position, it should have all the information it needs for layout.

    However, when it comes to autolayout, UIScrollViews behave in a unique way. The best description comes from this technical note. There are two options available including examples, but heres a summary of each.


    Mixed Approach

    You just need to add a UIView to the UIScrollView then add and position all your subviews in the UIView. This requires you manually setting the frame of the UIView and the contentSize on the UIScrollView.

    This is probably the easiest to use with the layout your trying to achieve. However, if the contentSize can change, you'll have to manually calculate and update the size.


    Pure Auto Layout Approach

    This option uses your autolayout constraints to determine the contentSize of the UIScrollView. This requires constraints going to all four edges of the UIScrollView and can not rely on the size of the UIScrollView.

    This option is tougher to use since you need to make sure you have enough constraints. In your case, you'll run into issues because there are no constraints to the top and bottom of the UIScrollView and there are no constraints that can be used to determin the width of the UIScrollView. However, this option is amazing when you have to deal with dynamic content as it will resize the contentSize as needed.


    Personally, I would go with the Pure Auto Layout Approach. It's ability to handle dynamic content sizes makes the extra constraint setup worth it.

    If you post what you want the final layout to be, I'll update my answer to reflect that.


    Update

    Based on the images you posted, this is the way I would organize the subviews using the Pure Auto Layout Approach. The main difference is that the UIScrollView is now a a subview of the UIViewControllers view.

    - UIView                                            (self.view)
      - UIScrollView                                    (scrollView)
        - UIView                                        (contentView)
          - UIImageView, UIButtons, UILabels, etc.
    

    scrollView needs constraints so its edges are 0px from self.view.

    contentView needs constraints so its edges are 0px from scrollView and that its width equals self.view. This is so the contentSize of the scrollView updates when you rotate the device.

    Next just position all your images and labels the way you want. The label will need to be constrained to the left and right so it can calculate its height. The important thing to note is contentView will determine its height based on the constraints for its subviews, so you will need constraints "linking" the top and bottom of the contentView. A simeple example would look like this.

    • contentView top to imageView top
    • imageView height == some value
    • imageView bottom to label top
    • label bottom to contentView bottom

提交回复
热议问题