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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值