ios7 中關於導航切換影響UIScrollView的問題

在 iOS 7 中,如果某個 UIViewController 的 self.view 第一個子視圖是 UIScollView, 同時當這個 UIViewController 被 push 或 initWithRootController 成爲 UINavigationController控制的Controller時,這個 UIViewController的 view 的子視圖 UIScollView 的所有子視圖, 都會被下移 64px。

   這個下移 64px 的前提是 navigationBar 和 statusBar 沒有隱藏。因爲爲 statusBar 默認的 Height 是 20px,而 navigatiBar  默認的 Height 是 44px。下面來比較一下

實例:

   不使用導航的界面跳轉

    1. 在 AppDelegate.m 文件中:

Obj-c代碼 收藏代碼
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  

  2. {  

  3.  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  

  4.  self.window.backgroundColor = [UIColor whiteColor];                                

  5.  //下面兩行爲增加的代碼                                                          

  6.  ViewController *rootViewController = [[ViewController alloc] init];  

  7.  [self.window setRootViewController:rootViewController];  

  8.  [self.window makeKeyAndVisible];  

  9.    return YES;  

  10. }  



2. 在 ViewController.m 中:


Obj-c代碼 收藏代碼
  1. - (void)viewDidLoad  

  2. {  

  3. [super viewDidLoad];  

  4. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30.0,                                                           64.0, 260.0, 300.0)];  

  5. [scrollView setBackgroundColor:[UIColor redColor]];  

  6. UIView *view = [[UIView alloc] initWithFrame:scrollView.bounds];  

  7. [view setBackgroundColor:[UIColor blueColor]];  

  8. [scrollView addSubview:view];  

  9. [self.view addSubview:scrollView];  

  10. }  


3. 運行後的結果:

wKioL1M02bjAygEmAABp-pZVp3I608.jpg


這種情況下,scrollView並未受影響。


4. 現在使用 UINavigationController,  將開始 AppDelegate.m 增加的那兩行代碼修改成:  



Obj-c代碼 收藏代碼
  1. ViewController *rootViewController = [[ViewController alloc] init];  

  2.  UINavigationController *navController = [[UINavigationController alloc]  

  3.                                 initWithRootViewController:rootViewController];  

  4.  [self.window setRootViewController:navController];  




5. 現在再次運行程序:

wKiom1M02uuD8SEiAAB48M0Pmhc504.jpg


如結果顯示, scrollView 背景色爲藍色的子視圖位置自動下移了。 而這個下移的距離剛好是 64.0px。


解決方法:

   第一種:在 ViewController 的 init 的方法中增加一行代碼:



Obj-c代碼 收藏代碼
  1. self.automaticallyAdjustsScrollViewInsets = NO;  



第二種: 讓UIScrollView 不要成爲 ViewController 的 View 的第一個子視圖。具體操作:將 viewDidLoad方法 修改成如下:



Obj-c代碼 收藏代碼
  1. - (void)viewDidLoad  

  2. {  

  3. [super viewDidLoad];  

  4. UIView *firstSubView = [[UIView alloc] initWithFrame:self.view.bounds];  

  5. [self.view addSubview:firstSubView];  

  6. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30.0,                                                           64.0, 260.0, 300.0)];  

  7. [scrollView setBackgroundColor:[UIColor redColor]];  

  8. UIView *view = [[UIView alloc] initWithFrame:scrollView.bounds];  

  9. [view setBackgroundColor:[UIColor blueColor]];  

  10. [scrollView addSubview:view];  

  11. [self.view addSubview:scrollView];  

  12. }  


第三種:將 UIScorllView 的子視圖上移 64.0px 。修改 viewDidLoad 方法:



Obj-c代碼 收藏代碼
  1. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30.0,                                                           64.0, 260.0, 300.0)];  

  2. [scrollView setBackgroundColor:[UIColor redColor]];  

  3. CGRect viewFrame = CGRectMake(0, -64.0, CGRectGetWidth(scrollView.frame),  

  4.                                                                                   CGRectGetHeight(scrollView.frame));  

  5. UIView *view = [[UIView alloc] initWithFrame: viewFrame];  

  6. [view setBackgroundColor:[UIColor blueColor]];  

  7. [scrollView addSubview:view];  

  8. [self.view addSubview:scrollView];  


第四種:設置導航欄的透明屬性。

self.navigationController.navigationBar.translucent = YES

改變導航欄透明度,也會影響,這個可以根據自己的實際需求進行調整。d


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章