在 iOS 7 中,如果某個(gè) UIViewController 的 self.view 第一個(gè)子視圖是 UIScollView, 同時(shí)當(dāng)這個(gè) UIViewController 被 push 或 initWithRootController 成為 UINavigationController控制的Controller時(shí),這個(gè) UIViewController的 view 的子視圖 UIScollView 的所有子視圖, 都會(huì)被下移 64px。
這個(gè)下移 64px 的前提是 navigationBar 和 statusBar 沒有隱藏。因?yàn)?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:運(yùn)行后的結(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)在再次運(yùn)行程序:
結(jié)果顯示, scrollView 背景色為藍(lán)色的子視圖位置自動(dòng)下移了。 而這個(gè)下移的距離剛好是 64.0px。*
解決方法:
- 第一種:在 ViewController 的 init 的方法中增加一行代碼:
self.automaticallyAdjustsScrollViewInsets = NO;
** iOS 7 viewcontroller新增屬性automaticallyAdjustsScrollViewInsets,**即是否根據(jù)按所在界面的navigationbar與tabbar的高度,自動(dòng)調(diào)整scrollview的 inset.
默認(rèn)值是YES,選擇YES表示你允許視圖控制器調(diào)整它內(nèi)部插入的滑動(dòng)視圖來應(yīng)對(duì)狀態(tài)欄,導(dǎo)航欄,工具欄,和標(biāo)簽欄所消耗的屏幕區(qū)域。如果你設(shè)置為NO呢,就代表呀你要自己調(diào)整你插入的滑動(dòng)視圖,比如你的視圖層次里面有多于一個(gè)的滑動(dòng)視圖。
- 第二種
iOS7以上系統(tǒng),
self.navigationController.navigationBar.translucent
默認(rèn)為YES,self.view.frame.origin.y從0開始(屏幕最上部)。
此時(shí)若是添加代碼self.edgesForExtendedLayout = UIRectEdgeNone
(iOS7.0以后方法);self.view.frame.origin.y會(huì)下移64像素至navBar下方開始。
self.edgesForExtendedLayout = UIRectEdge.None;
將view下移64,另外如果有tabBar,高度會(huì)縮減40,無需我們手動(dòng)設(shè)置
- 第三種:設(shè)置導(dǎo)航欄的透明屬性。
self.navigationController.navigationBar.translucent = NO;
改變導(dǎo)航欄透明度,也會(huì)影響.
iOS7之后也增加了一個(gè)
self.tabBarController.tabBar.translucent
的屬性,默認(rèn)為YES
。
當(dāng)應(yīng)用同時(shí)使用navBar和TabBar的時(shí)候,
設(shè)置self.tabBarController.tabBar.translucent=NO
,
并且self.navigationController.navigationBar.translucent=NO
時(shí)候,
得到self.view.frame—>{{0, 64}, {320, 455}}。視圖的高度也改變?yōu)閚avBar和tabBar之間的455像素。
當(dāng)self.navigationController.navigationBar.translucent=YES
并且self.tabBarController.tabBar.translucent=NO
的時(shí)候self.view.frame—>{{0, 0}, {320, 519}};其都為YES的時(shí)候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后顏色還會(huì)變,說明設(shè)置的是同一個(gè)UINavigationBar,) - (2)在子集中用
self.navigationController.navigationBar.barTintColor
修改Navigationbar顏色
備注
[UINavigationBar appearance]的方法只能在Appdelegate,UITabBarController里用,在UINavigationController的子頁面中只能通過self.navigationController修改NagivationBar的屬性.