How do I control the relative position of views, especially I wish my app to run on 3.5 inch display and 4 inch display seamlessly?
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 option – if you're using Interface Builder you can experiment with each of the options to see which one suits your needs. 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.
There is no equivalent or relative and linear layouts. The UI elements have autoresizing masks which define how will they be moved/stretched when their superview is resized (e.g. screen rotation). You can also use layout constraints for positioning if you intend to build your app for iOS 6+. If you can't solve the repositioning using these tools, you should change the frames of the UI elements in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With