Adjusting UIView Height Inside UIStackView Control

前端 未结 4 1151
Happy的楠姐
Happy的楠姐 2020-12-06 09:59

I am using a UIStackView in iOS 9 SDK. The height of the stackview is 44 points. I have a UILabel and UIView inside the StackView as shown below:

Now, I wa

4条回答
  •  粉色の甜心
    2020-12-06 10:28

    If you are trying to achieve a 20x20 square like the one below:

    Then all you have to do is add another UIView to your green view. Make that UIView's size 20x20pts. Then change the small view background to green and the larger view's background to clear.

    That said, this solution doesn't require a stackview and possibly over-complicates your UI. I would recommend just using autolayout instead.

    Stackviews are most useful when

    1. you want to compensate for a change in device orientation,
    2. want to stack a lot of things on top of each other,
    3. want to create a very complicated layout with many elements but that has patterns you can repeat, or
    4. simply want to be able to remove an element or add an element in with the sizing correcting itself

    That list isn't exhaustive, but should give you an idea of when and why you'd use a stack view.

    Outside of a prototype cell

    This behavior is trivial. You can simply set the constraints on your view: '

    And then set the constraints on the stackview:

    Hope this helps, cheers!

提交回复
热议问题