您的当前位置:首页正文

iOS tableview起始位置在状态栏下方开始

来源:要发发知识网

今天遇到了一个很奇怪的问题,在tableview页面隐藏导航栏之后,tableview其实位置不对.(不多赘述,直接看代码吧!)

修改前代码:

- (void)viewDidLoad {

[superviewDidLoad];//只有隐藏了导航栏才会出现这个bug,如果导航栏没有隐藏,就没有任何问题[self.navigationControllersetNavigationBarHidden:YES];

self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KeyWidth, KeyHeight-44) style:UITableViewStyleGrouped];

self.tableView.backgroundColor = LSBgColor;

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

self.tableView.delegate = self;

self.tableView.dataSource = self;

//设置滚动条位置

self.tableView.scrollIndicatorInsets = self.tableView.contentInset;

//注册行 cell

[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LSPraiseWallCell class]) bundle:nil] forCellReuseIdentifier:LSPraiseWallCellId];

//先随意设置一个占位高度

self.tableView.rowHeight = 10;

self.edgesForExtendedLayout = UIRectEdgeNone;

[self.view addSubview:self.tableView];

}

代码运行效果如下:

请注意,tableview的其实位置此时并不是从(0,0)开始的.

如果遇到这种情况,最简单的办法就是在tableview初始化之前加上下面这段代码

 //在self.view上添加子视图

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

[self.view addSubview:view];

修改后效果图:

快试一下,是不是问题已经解决了呢?

恩,解决了就好,不用谢,我也是顺手记下来而已,嘻嘻!