一、沒有包裝任何 導航控制器 或者UITabBarController
1.控制器的view是UIScrollView/UITableView/UICollectionView時(控制器是UITableViewController的時候)
- (void)viewDidLoad
{
[super viewDidLoad];
// #ifdef __IPHONE_7_0是判斷是否運行在Xcode5環境下,如果在Xcode5環境下才有下面的代碼
#ifdef __IPHONE_7_0
if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
}
#endif
}
2.控制器的view是普通的UIView,非UIScrollView
#ifdef __IPHONE_7_0
- (void)viewDidLayoutSubviews
{
// iOS7 && 沒有包裝導航控制器
if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0 &&self.navigationController == nil) {
CGFloat top = [self.topLayoutGuide length];
// 是否能滾動
if ([self.view isKindOfClass:[UIScrollView class]]) {
UIScrollView *scroll = (UIScrollView *)self.view;
scroll.contentInset = UIEdgeInsetsMake(top, scroll.contentInset.left, scroll.contentInset.bottom, scroll.contentInset.right);
} else {
CGRect bounds = self.view.bounds;
bounds.origin.y = - top;
self.view.bounds = bounds;
}
}
}
#endif
二、包裝有導航控制器的情況
1> 控制器的view不是UIScrollView
#ifdef __IPHONE_7_0
if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
#endif
2> 控制器的view是UIScrollView
不需要寫額外的代碼適配
三、其他情況(上述情況不用死機,只要掌握以下幾點規律)
1.想讓view的內容往下挪動
1> UIView設置bounds的y值
2> UIScrollView設置contentInset的top值
2.防止子控制器的view被導航欄或者tabbar遮住
self.edgesForExtendedLayout = UIRectEdgeNone;
四、多控制器嵌套處理
1.當多重控制器嵌套的時候,最合理的方案是:UITabBarController內部嵌套UINavigationController
2.當UITableViewController的直接父控制器是UINavigationController時,不需要編寫任何適配代碼
3.其他非UITableViewController需要加上適配代碼
#ifdef __IPHONE_7_0
if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
#endif