ios7 UIScrollView 尺寸問題

假設在UINavigationController內設置一個UIViewControlller,而UIViewController的第一個子視圖是UIScrollView的話,UIScrollview裏面所有的subView都會發生下移,如圖所看到的
ios7 <wbr>UIScrollView <wbr>尺寸問題
代碼爲

- (void)viewDidLoadhtml

{ios

    [super viewDidLoad];url

 

    UIScrollView *tempScroll = [[UIScrollView allocinitWithFrame:CGRectMake(064320200)];spa

    [tempScroll setBackgroundColor:[UIColor grayColor]];orm

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

    [self.view addSubview:tempScroll];blog

 

    UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];get

    [tempButton setBackgroundColor:[UIColor redColor]];it

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

    [tempButton setFrame:CGRectMake(80080100)];

    

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

    [tempScroll addSubview:tempButton];

}

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

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

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

修正方案有兩個

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

    UIView *targetView = self.view;

    while (targetView.subviews.count >0 && ![targetView isKindOfClass:[UIScrollView class]]) {

        targetView = [targetView.subviews objectAtIndex:0];

    }

    if ([targetView isKindOfClass:[UIScrollView class]]) {

        NSLog(@"you are a scrollview");

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

        tempSize.height -= 64;

        [(UIScrollView *)targetView setContentSize:tempSize];

        for (UIView *subView in targetView.subviews) {

            CGRect tempRect = subView.frame;

            tempRect.origin.y -= 64;

            [subView setFrame:tempRect];

        }

 

    }

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

- (void)viewDidLoad

{

    [super viewDidLoad];

 

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

    [self.view addSubview:tempBackGround];

    

    UIScrollView *tempScroll = [[UIScrollView allocinitWithFrame:CGRectMake(064320200)];

    [tempScroll setBackgroundColor:[UIColor grayColor]];

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

    [self.view addSubview:tempScroll];

 

    UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [tempButton setBackgroundColor:[UIColor redColor]];

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

    [tempButton setFrame:CGRectMake(80080100)];

    

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

    [tempScroll addSubview:tempButton];


 

}

通過了修正如圖所看到的

ios7 <wbr>UIScrollView <wbr>尺寸問題

相關文章
相關標籤/搜索