App一般自己都会有一个UINavigationController,顶部TableView如果有tableHeaderView如果设置起始位置是(0,0)是在导航栏的下面的.但是有时需求要求从屏幕的顶部开始. 查阅了很多文章都是说调整automaticallyAdjustsScrollViewInsets=NO.但在实际操作过程中没有达到想要的效果.
1 .例如下面示例.
@implementation TJTestadcViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"测试view";
self.automaticallyAdjustsScrollViewInsets = NO;
// self.navigationController.navigationBar.translucent = YES;
self.view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.tableView];
}
@end
代码中 实现了self.automaticallyAdjustsScrollViewInsets = NO; 但是实际效果如下
self.view 和tableview的(0,0) 都是从导航栏下面开始.无法满足需求
然后,查阅其他资料,看到navigationBar的translucent属性(设置是否透明) 然后通过设置这个属性.
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"测试view";
self.automaticallyAdjustsScrollViewInsets = NO;
//设置透明
self.navigationController.navigationBar.translucent = YES;
self.view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.tableView];
}
查看层级关系图,发现self.view和tableView的(0,0)坐标 ,从屏幕的顶部开始.达到效果.
然后 又设置 一些相关 self.navigationController.navigationBar.translucent = NO;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"测试view";
self.automaticallyAdjustsScrollViewInsets = NO;
//设置不透明
self.navigationController.navigationBar.translucent = NO;
self.view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.tableView];
}
从上面的效果可以看出来 self.automaticallyAdjustsScrollViewInsets = NO; 无法影响 tableView和self.view 是否从屏幕顶部开始.而是由self.navigationController.navigationBar.translucent 控制的.如有不同意见,请多多指教.