如果我的項目支持iOS版本4.3及更高版本,如何在iOS6 中啓用自動佈局,但將其禁用到較低版本?在iOS 6中啓用Autolayout,在iOS6中禁用Autolayout
回答
如果您正在使用故事板,創建兩個故事板,一個具有自動佈局功能,另一個具有自動佈局功能。然後檢查的一種方法是基於os版本加載任意一個。
我用這個小宏
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:(v) options:NSNumericSearch] != NSOrderedAscending)
這應該工作太(在某些情況下則上述可能更好):
if ([NSLayoutConstraint class]) {
// >= 6.0
} else {
// < 6.0
}
,然後在我的appDelegate:
UIStoryboard *mainStoryboard = nil;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0"))
{
mainStoryboard = [UIStoryboard storyboardWithName:@"AutoLayoutStoryboard" bundle:nil];
}
else
{
mainStoryboard = [UIStoryboard storyboardWithName:@"NoAutoLayoutStoryboard" bundle:nil];
}
//load initial view controller
UIViewController *rootView = [mainStoryboard instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = rootView;
[self.window makeKeyAndVisible];
ADD:如果您使用的是筆尖,並且只想爲iOS6啓用自動佈局,您可以創建一個故事板(或筆尖)自動佈局,並使用上面的版本檢查加載它們
你如何設置'mainStoryboard'?在App Delegate中設置故事板的代碼是什麼? – JimmyJammed
@JamesHickman我已經更新了上面的代碼,以顯示我是如何做到的。希望能幫助到你... –
要麼你應該使用兩個xib文件,或者你應該在編寫自動佈局和約束代碼之前以編程方式檢查它。
如何以編程方式檢查它?我一直在使用xib文件,順便說一句。 :) –
有多種方式:從UIDevice類,你可以有版本字符串。通過檢查respondsToSelector,您可以檢查特定屬性的可用性。 – Apurv
- 1. ios6中的Autolayout
- 2. ios - 在Xcode 6上使用UIScrollview w/autolayout
- 3. Autolayout在iOS 7中調整完美但不在iOS中6
- 4. UIScrollView不適用於Autolayout(iOS 6)
- 5. iOS 6 - 從Autolayout中免除一個ViewController
- 6. 動態尺寸與iOS 6中的Autolayout
- 7. 何時在iOS AutoLayout中使用乘數?
- 8. 如何以編程方式啓用iOS 6的「AutoLayout」?
- 9. Xcode 6 Autolayout- UITableView列
- 10. iOS iPhone 6 Autolayout - 全寬和高度
- 11. iOS 6 AutoLayout縮放和翻譯動畫
- 12. iOS 7 UITableViewCell Autolayout
- 13. iOS 7 Autolayout
- 14. iOS AutoLayout約束
- 15. iOS限制 - Autolayout
- 16. Autolayout UICollectionView iOS 8
- 17. iOS 7 Autolayout不起作用
- 18. iOS中的Autolayout編程式
- 19. 使用AutoLayout在UIScrollview中居中UILabel
- 20. XCode 6.0.1 ios 7 Autolayout
- 21. iOS Autolayout和UIToolbar/UIBarButtonItems
- 22. Autolayout限制在iOS 8中工作,在iOS中休息9
- 23. 如何在iOS中的故事板中使用AutoLayout?
- 24. AutoLayout與iOS 6.0以下 - iOS
- 25. GMSMapView中的AutoLayout
- 26. AutoResizingView中的AutoLayout
- 27. 如何在swift中重用tableviewCell和autoLayout?
- 28. 使用Autolayout在UITableViewCell中的單個UIImageView
- 29. 在UIScrollView中嵌入UIView使用Autolayout
- 30. iOS 8 AutoLayout scrollview在iPhone 6上水平滾動加
另外,考慮到非自動佈局還沒有被棄用(即它仍然被支持),你總是可以考慮完全關閉自動佈局並調用它一天。只提供一個非自動佈局的應用程序。或者,您是否計劃通過iOS 6用戶的自動佈局啓用某些功能,而不向iOS 5用戶展示?但是,如果你經歷了很多工作才能讓應用程序適用於iOS 5,我不確定爲什麼你也要做一個自動佈局版本。 – Rob