导航控制器切换影响UIScrollView布局

ios7 导航控制器切换影响UIScrollView布局的问题:


如结果显示, scrollView 的子视图位置自动下移了。 而这个下移的距离刚好是 64.0px。


解决方法:

=====================

   第一种:在 ViewController 的 init 的方法中增加一行代码:


Obj-c代码 收藏代码

self.automaticallyAdjustsScrollViewInsets = NO;  


=====================

第二种: 让UIScrollView 不要成为 ViewController 的 View 的第一个子视图。具体操作:将 viewDidLoad方法 修改成如下:


Obj-c代码 收藏代码

- (void)viewDidLoad  

{  

[super viewDidLoad];  

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

[self.view addSubview:firstSubView];  

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

[scrollView setBackgroundColor:[UIColor redColor]];  

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

[view setBackgroundColor:[UIColor blueColor]];  

[scrollView addSubview:view];  

[self.view addSubview:scrollView];  

}  



=====================

第三种:将 UIScorllView 的子视图上移 64.0px 。修改 viewDidLoad 方法:


Obj-c代码 收藏代码

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

[scrollView setBackgroundColor:[UIColor redColor]];  

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

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

[view setBackgroundColor:[UIColor blueColor]];  

[scrollView addSubview:view];  

[self.view addSubview:scrollView];  



=====================

第四种:设置导航栏的透明属性。

self.navigationController.navigationBar.translucent = YES

改变导航栏透明度,也会影响,这个可以根据自己的实际需求进行调整。

原文地址:https://www.cnblogs.com/luqinbin/p/5090980.html