Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the iOS equivalence of Android RelativeLayout/LinearLayout?

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?

like image 515
Fermat's Little Student Avatar asked Mar 23 '23 13:03

Fermat's Little Student


2 Answers

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.

like image 137
TwoStraws Avatar answered Apr 26 '23 04:04

TwoStraws


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.

like image 20
Levi Avatar answered Apr 26 '23 04:04

Levi