What is the best way to take advantage of the new auto layout features of iOS 6 while still providing compability with older devices on earlier versions of iOS?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- Popover segue to static cell UITableView causes co
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
Do you really need two targets? I got it working like this, I have 2 storyboard like Imre Kelényi said, one with auto layouts enabled and the other without, then in the app delegate i just check which version they are using and select the right storyboard:
Having 2 targets works aswell but seems like overkill to me
I've found setting the Xibs main view size to Freeform, and then using Autosizing works a treat. No messing about in code for a view issue.
Try to use RRAutoLayout: https://github.com/RolandasRazma/RRAutoLayout It's iOS6 AutoLayout backport to iOS5.
If the layout differences are not large, it's a lot easier to use Springs and Struts to position elements.
Autolayout can be enabled or disabled on each .storyboard or .xib file. Just select the particular file and modify the "Use Autolayout" property using the File inspector in Xcode:
Using autolayout enabled interface files with the deployment target set to an iOS version prior to 6.0 results in compilation errors, e.g.:
One of your options to use autolayout in a project and still preserve compatibility with iOS4-5 is to create two targets: one for deployment target iOS 6.0 and one for an earlier iOS version, e.g.:
You can create two versions for each of your storyboard and XIB files as well and use the autolayout enabled with the 6.0 target and the other with the legacy target, e.g.:
You then add MainStoryBoardAutoSize to the iOS6 target's Build phases and the other file to the iOS4 target. You can learn more about using multiple targets here.
For me, the cost of the added complexity of maintaining multiple targets and interface files seems to outweigh the benefits of using autolayout. Except for a few special cases, you are probably much better to use plain old auto sizing (or layoutSubViews from code) exclusively if iOS4-5 compatibility is required.
Inspired by @marchinram's one target idea, this is the solution I finally came up with. Two storyboards, one for struts-and-springs and one for autolayout. In the target summary, I set the autolayout storyboard as the default. Then, in the appDelegate, I check whether I need to load the pre-6.0 struts-and-springs storyboard after all:
Also, I set the deployment target of the struts-and-springs storyboard to iOS 5.1, and that of the autolayout storyboard to Project SDK(iOS 6.0).
I really wanted to do the switch before the default in storyboard is loaded, in willFinishLaunchingWithOptions: but that results in an 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named NSLayoutConstraint' no matter what I tried.