在 iOS 7 中,如果某個 UIViewController 的 self.view 第一個子視圖是 UIScollView膏孟, 同時當(dāng)這個 UIViewController 被 push 或 initWithRootController 成為 UINavigationController控制的Controller時,這個 UIViewController的 view 的子視圖 UIScollView 的所有子視圖拌汇, 都會被下移 64px柒桑。
這個下移 64px 的前提是 navigationBar 和 statusBar 沒有隱藏。因為 statusBar 默認(rèn)的 Height 是 20px噪舀,而 navigatiBar 默認(rèn)的 Height 是 44px魁淳。
下面來比較一下
eg:
不使用導(dǎo)航的界面跳轉(zhuǎn)
1:在 AppDelegate.m 文件中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//下面兩行為增加的代碼
ViewController *rootViewController = [[ViewController alloc] init];
[self.window setRootViewController:rootViewController];
[self.window makeKeyAndVisible];
return YES;
}
2:在 ViewController.m 中:
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30.0, 64.0, 260.0, 300.0)];
[scrollView setBackgroundColor:[UIColor redColor]];
UIView *view = [[UIView alloc] initWithFrame:scrollView.bounds];
[view setBackgroundColor:[UIColor blueColor]];
[scrollView addSubview:view];
[self.view addSubview:scrollView];
}
3:運行后的結(jié)果:
scrollView并未受影響。
4:現(xiàn)在使用 UINavigationController, 將開始 AppDelegate.m 增加的那兩行代碼修改成:
ViewController *rootViewController = [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
[self.window setRootViewController:navController];
5:現(xiàn)在再次運行程序:
結(jié)果顯示与倡, scrollView 背景色為藍(lán)色的子視圖位置自動下移了界逛。 而這個下移的距離剛好是 64.0px。*
解決方法:
- 第一種:在 ViewController 的 init 的方法中增加一行代碼:
self.automaticallyAdjustsScrollViewInsets = NO;
** iOS 7 viewcontroller新增屬性automaticallyAdjustsScrollViewInsets纺座,**即是否根據(jù)按所在界面的navigationbar與tabbar的高度息拜,自動調(diào)整scrollview的 inset.
默認(rèn)值是YES,選擇YES表示你允許視圖控制器調(diào)整它內(nèi)部插入的滑動視圖來應(yīng)對狀態(tài)欄,導(dǎo)航欄少欺,工具欄喳瓣,和標(biāo)簽欄所消耗的屏幕區(qū)域。如果你設(shè)置為NO呢赞别,就代表呀你要自己調(diào)整你插入的滑動視圖畏陕,比如你的視圖層次里面有多于一個的滑動視圖。
- 第二種
iOS7以上系統(tǒng)氯庆,
self.navigationController.navigationBar.translucent
默認(rèn)為YES蹭秋,self.view.frame.origin.y從0開始(屏幕最上部)。
此時若是添加代碼self.edgesForExtendedLayout = UIRectEdgeNone
(iOS7.0以后方法);self.view.frame.origin.y會下移64像素至navBar下方開始堤撵。
self.edgesForExtendedLayout = UIRectEdge.None仁讨;
將view下移64,另外如果有tabBar实昨,高度會縮減40洞豁,無需我們手動設(shè)置
- 第三種:設(shè)置導(dǎo)航欄的透明屬性。
self.navigationController.navigationBar.translucent = NO;
改變導(dǎo)航欄透明度荒给,也會影響.
iOS7之后也增加了一個
self.tabBarController.tabBar.translucent
的屬性丈挟,默認(rèn)為YES
。
當(dāng)應(yīng)用同時使用navBar和TabBar的時候,
設(shè)置self.tabBarController.tabBar.translucent=NO
,
并且self.navigationController.navigationBar.translucent=NO
時候志电,
得到self.view.frame—>{{0, 64}, {320, 455}}曙咽。視圖的高度也改變?yōu)閚avBar和tabBar之間的455像素。
當(dāng)self.navigationController.navigationBar.translucent=YES
并且self.tabBarController.tabBar.translucent=NO
的時候self.view.frame—>{{0, 0}, {320, 519}}挑辆;其都為YES的時候self.view.frame—>{{0, 0}, {320, 568}}例朱;
設(shè)置導(dǎo)航欄NavigationBar的背景顏色:
在appdelegate里創(chuàng)建UINavigationController后,設(shè)置:
- (1) setBarTintColor : 設(shè)置NagivationBar的顏色 也可以用 :
[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
(在
UINavigationController執(zhí)行pushViewController的界面里再次setBarTintColor后顏色還會變,說明設(shè)置的是同一個UINavigationBar,) - (2)在子集中用
self.navigationController.navigationBar.barTintColor
修改Navigationbar顏色
備注
[UINavigationBar appearance]的方法只能在Appdelegate,UITabBarController里用,在UINavigationController的子頁面中只能通過self.navigationController修改NagivationBar的屬性.