在navigationControl的视图上添加scrollView的出现向下偏移的问题,以及导航条背景色的设置

在 iOS 7 中,如果某个 UIViewController 的 self.view 第一个子视图是 UIScollView, 同时当这个 UIViewController 被 push 或 initWithRootController 成为 UINavigationController控制的Controller时,这个 UIViewController的 view 的子视图 UIScollView 的所有子视图, 都会被下移 64px。
这个下移 64px 的前提是 navigationBar 和 statusBar 没有隐藏。因为 statusBar 默认的 Height 是 20px,而 navigatiBar 默认的 Height 是 44px。


下面来比较一下
eg:

不使用导航的界面跳转
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:运行后的结果:
scrollView并未受影响。

4:现在使用 UINavigationController, 将开始 AppDelegate.m 增加的那两行代码修改成:

ViewController *rootViewController = [[ViewController alloc] init];  
 UINavigationController *navController = [[UINavigationController alloc]  
                                initWithRootViewController:rootViewController];  
 [self.window setRootViewController:navController];  
 

5:现在再次运行程序:
结果显示, scrollView 背景色为蓝色的子视图位置自动下移了。 而这个下移的距离刚好是 64.0px。*


解决方法:

  • 第一种:在 ViewController 的 init 的方法中增加一行代码:

self.automaticallyAdjustsScrollViewInsets = NO;
** iOS 7 viewcontroller新增属性automaticallyAdjustsScrollViewInsets,**即是否根据按所在界面的navigationbar与tabbar的高度,自动调整scrollview的 inset.
默认值是YES,选择YES表示你允许视图控制器调整它内部插入的滑动视图来应对状态栏,导航栏,工具栏,和标签栏所消耗的屏幕区域。如果你设置为NO呢,就代表呀你要自己调整你插入的滑动视图,比如你的视图层次里面有多于一个的滑动视图。

  • 第二种

iOS7以上系统,self.navigationController.navigationBar.translucent默认为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,无需我们手动设置

  • 第三种:设置导航栏的透明属性。

self.navigationController.navigationBar.translucent = NO;
改变导航栏透明度,也会影响.


iOS7之后也增加了一个self.tabBarController.tabBar.translucent的属性,默认为YES
当应用同时使用navBar和TabBar的时候,
设置self.tabBarController.tabBar.translucent=NO,
并且self.navigationController.navigationBar.translucent=NO时候,
得到self.view.frame—>{{0, 64}, {320, 455}}。视图的高度也改变为navBar和tabBar之间的455像素。
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}};


设置导航栏NavigationBar的背景颜色:

在appdelegate里创建UINavigationController后,设置:

  • (1) setBarTintColor : 设置NagivationBar的颜色 也可以用 :
    [[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];(在
    UINavigationController执行pushViewController的界面里再次setBarTintColor后颜色还会变,说明设置的是同一个UINavigationBar,)
  • (2)在子集中用self.navigationController.navigationBar.barTintColor修改Navigationbar颜色
    备注
    [UINavigationBar appearance]的方法只能在Appdelegate,UITabBarController里用,在UINavigationController的子页面中只能通过self.navigationController修改NagivationBar的属性.

你可能感兴趣的:(在navigationControl的视图上添加scrollView的出现向下偏移的问题,以及导航条背景色的设置)