Relativelayout or LinearLayout in ios iphone development?

前端 未结 6 672
再見小時候
再見小時候 2021-02-08 15:23

I want to add a subview in the top of my view, I have to recalculate the origin y value for all of other views and re-position them to leave space for the new added view.

<
6条回答
  •  一整个雨季
    2021-02-08 15:37

    As of iOS 9 you can use UIStackView, which works very similarly to LinearLayout: you add views and the stack view arranges them as needed based on your sizing preferences:

    • Fill will leave three of them their natural size, and make the fourth one take up the most space. It uses Auto Layout's content hugging priority to decide which one to stretch.

    • Fill Equally will make each subview the same size so they fill all the space available to the stack view.

    • Fill Proportionally uses the intrinsic content size of each subview to resize them by an equal amount. So view 1 was designed to have twice as much height as views 2, 3 and 4, that ratio will remain when they are resized – all the subviews get proportionally bigger or smaller.

    • Equal Spacing does not resize the subviews, and instead resizes the spacing between the subviews to fill the space.

    • Equal Centering is the most complicated, but for many people also the most aesthetically pleasing. It attempts to ensure the centers of each subview are equally spaced.

    You can also set spacing between views in the stack view, adding some padding.

    WARNING: When adding stack view child views in code you should always use addArrangedSubview() like this:

    stackView.addArrangedSubview(someView)
    

    If you try to use plain old addSubview() it won't work correctly, because the stack view won't know to arrange it.

    As for removing, you need to be careful to use stackView.removeArrangedSubview(someView) and someView.removeFromSuperview() otherwise the view won't be removed correctly.

    You might find my UIStackView tutorial useful.

提交回复
热议问题