问题
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?
回答1:
Check out this: iOS 6 apps - how to deal with iPhone 5 screen size? and How to add iPhone 5 large screen support to iOS apps in Xcode?
But it's done for you for the most part with auto layout. Click on your project in Xcode and go to the Summary tab to add the different screen size launch screen for your app.
回答2:
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.
回答3:
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.
回答4:
Auto-Layout system ≫ RelativeLayout
UIStackView ≈ LinearLayout
IMO. UIStackView was not well designed
- can't easily add extra constraints between subviews/subview and superview
it's too often to produce conflicts of constraints
x. iOS 9+ only
So i wrote AutoLinearLayoutView, a replacement of UIStackView.
Demo screenshot
Checkout from Github AutoLinearLayoutView
来源:https://stackoverflow.com/questions/18622329/what-is-the-ios-equivalence-of-android-relativelayout-linearlayout