在navigationControl的視圖上添加scrollView的出現(xiàn)向下偏移的問題,以及導(dǎo)航條背景色的設(shè)置

在 iOS 7 中,如果某個 UIViewController 的 self.view 第一個子視圖是 UIScollView, 同時當(dāng)這個 UIViewController 被 push 或 initWithRootController 成為 UINavigationController控制的Controller時,這個 UIViewController的 view 的子視圖 UIScollView 的所有子視圖, 都會被下移 64px。
這個下移 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)色的子視圖位置自動下移了。 而這個下移的距離剛好是 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的屬性.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容