在 iOS 6 中启用自动布局,同时保持向后兼容 iOS 5

Enabling auto layout in iOS 6 while remaining backwards compatible with iOS 5(在 iOS 6 中启用自动布局,同时保持向后兼容 iOS 5)
本文介绍了在 iOS 6 中启用自动布局,同时保持向后兼容 iOS 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在利用 iOS 6 新的自动布局功能的同时,还能在早期版本的 iOS 上提供与旧设备的兼容性的最佳方式是什么?

解决方案

可以在每个 .storyboard 或 .xib 文件上启用或禁用自动布局.只需选择特定文件并使用 Xcode 中的文件检查器修改使用自动布局"属性:

使用启用自动布局的接口文件并将部署目标设置为 6.0 之前的 iOS 版本会导致编译错误,例如:

<块引用>

MainStoryboard.storyboard:3 中的错误:iOS 6.0 之前版本的自动布局

在项目中使用自动布局并仍然保持与 iOS4-5 的兼容性的一种选择是创建两个 指出的那样,如果您从代码加载情节提要文件并且不使用主要Storyboard"设置在Xcode中设置初始storyboard,可以使用单个target.

对我来说,维护多个目标和接口文件所增加的复杂性成本似乎超过了使用自动布局的好处.除了少数特殊情况,如果需要 iOS4-5 兼容性,您可能最好使用普通的旧自动调整大小(或代码中的 layoutSubViews).

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?

解决方案

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.:

Error in MainStoryboard.storyboard:3: Auto Layout on iOS Versions prior to 6.0

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.

EDIT: As marchinram's answer points out, if you load you storyboard files from code and do not use the "Main Storyboard" setting in Xcode to set the initial storyboard, you can use a single target.

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.

这篇关于在 iOS 6 中启用自动布局,同时保持向后兼容 iOS 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

问题描述:H5页面,ios点击input框,输入内容时,页面会自动放大,且失焦后需手动放小,要怎么处理呢? 解决办法 meta name="viewport" content="width=device-width, initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" width=device
AWS DynamoDB Batch Get Request - iOS(AWS DynamoDB 批量获取请求 - iOS)
Querying DynamoDB on non-key attributes(在非关键属性上查询 DynamoDB)
DynamoDB auto incremented ID amp; server time (iOS SDK)(DynamoDB 自动递增 ID amp;服务器时间(iOS SDK))
Where to find a clear explanation about swift alert (UIAlertController)?(哪里可以找到关于 swift alert (UIAlertController) 的清晰解释?)
Facebook Requests Dialog: Frictionless Requests in native iOS app possible?(Facebook 请求对话框:本机 iOS 应用程序中的无摩擦请求可能吗?)