ios7 UIScrollView 尺寸问题

来源:互联网 发布:淘宝信用贷款逾期t 1 编辑:程序博客网 时间:2024/05/17 10:40
如果在UINavigationController内设置一个UIViewControlller,而UIViewController的第一个子视图是UIScrollView的话,UIScrollview里面所有的subView都会发生下移,如图所示 UIScrollView 尺寸问题 title=ios7 UIScrollView 尺寸问题> 代码为

- (void)viewDidLoad

{

 [super viewDidLoad];

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

 [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(80, 0, 80, 100)];

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

 [tempScroll addSubview:tempButton];

}

经过验证性的代码,我发现ios7有一个机制

在navigationBar,以及statusBar都显示的情况下,Navigation的当前VC,他的VC的view的子视图树的根部的第一个子视图,如果是Scrollview的话,这个scrollview的所有子视图都会被下移64个像素。

发现了这个机制之后,怎么去修正呢?

修正方案有两个

1、把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];

 }

 }

2、把scrollView更改地位,是它不是子视图树的根部第一个子视图。

- (void)viewDidLoad

{

 [super viewDidLoad];

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

 [self.view addSubview:tempBackGround];

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

 [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(80, 0, 80, 100)];

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

 [tempScroll addSubview:tempButton];

 

}

经过了修正如图所示

0 0