ios7 UIScrollView 尺寸問題------UIScrollView的子視圖整體下移

如果在UINavigationController內設置一個UIViewControlller,而UIViewController的第一個子視圖是UIScrollView的話,UIScrollview裏面所有的subView都會發生下移,如圖所示


ios7 <wbr>UIScrollView <wbr>尺寸問題
代碼爲

- (void)viewDidLoad

{

   [superviewDidLoad];

 

   UIScrollView *tempScroll = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,64,320, 200)];

   [tempScrollsetBackgroundColor:[UIColor grayColor]];

   [tempScroll setContentSize:CGSizeMake(self.view.bounds.size.width,self.view.bounds.size.height)];

    [self.view addSubview:tempScroll];

 

   UIButton*tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

   [tempButtonsetBackgroundColor:[UIColor redColor]];

   [tempButton setTitle:@"subView A"forState:UIControlStateNormal];

    [tempButtonsetFrame:CGRectMake(80,0,80, 100)];

   

   NSLog(@"%d",tempScroll.subviews.count);

    [tempScrolladdSubview:tempButton];

}

經過驗證性的代碼,我發現ios7有一個機制

在navigationBar,以及statusBar都顯示的情況下,Navigation的當前VC,他的VC的view的子視圖樹的根部的第一個子視圖,如果是Scrollview的話,這個scrollview的所有子視圖都會被下移64個像素。

發現了這個機制之後,怎麼去修正呢?

修正方案有兩個

1、把scrollview的所有子視圖上移64個像素。

   UIView *targetView = self.view;

   while (targetView.subviews.count>0&& ![targetViewisKindOfClass:[UIScrollViewclass]]) {

       targetView =[targetView.subviews objectAtIndex:0];

    }

   if ([targetView isKindOfClass:[UIScrollViewclass]]) {

      NSLog(@"you are ascrollview");

      CGSize tempSize = ((UIScrollView *)targetView).contentSize;

       tempSize.height -= 64;

       [(UIScrollView *)targetView setContentSize:tempSize];

      for(UIView *subView intargetView.subviews) {

         CGRect tempRect =subView.frame;

          tempRect.origin.y -=64;

          [subView setFrame:tempRect];

       }

 

   }


2、把scrollView更改地位,是它不是子視圖樹的根部第一個子視圖。

- (void)viewDidLoad

{

   [superviewDidLoad];

 

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

    [self.view addSubview:tempBackGround];

   

   UIScrollView *tempScroll = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,64,320, 200)];

   [tempScrollsetBackgroundColor:[UIColor grayColor]];

   [tempScroll setContentSize:CGSizeMake(self.view.bounds.size.width,self.view.bounds.size.height)];

    [self.view addSubview:tempScroll];

 

   UIButton*tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

   [tempButtonsetBackgroundColor:[UIColor redColor]];

   [tempButton setTitle:@"subView A"forState:UIControlStateNormal];

    [tempButtonsetFrame:CGRectMake(80,0,80, 100)];

   

   NSLog(@"%d",tempScroll.subviews.count);

    [tempScrolladdSubview:tempButton];

}




原文:http://blog.sina.com.cn/s/blog_693850220101ev1i.html

發佈了7 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章