关于UINavigationController与UITableView聚合的发现
今天写iOS项目时,发现了几个问题,于是停下项目,好好地进行了一番研究(已发现文中问题的亲们可跳过,哈哈): 当我们的视图(控制器的视图)中有导航条,即导航控制器自动为我们添加的导航条时。 通过导航控制器来控制视图: self.window.rootViewController=[[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
我们在控制器的视图中添加子视图时,子视图显示内容的y坐标是相对于视图的最顶端来说的。如图: 添加视图的代码如下: - (void)viewDidLoad {
[super viewDidLoad];
CGFloat width=self.view.frame.size.width;
self.view.backgroundColor=[UIColor whiteColor];
UIView* view1=[[UIView alloc] init];
view1.frame=CGRectMake(0,0,width,64);
[self.view addSubview:view1];
view1.backgroundColor=[UIColor redColor];
UIView* view2=[[UIView alloc] init];
view2.frame=CGRectMake(0,64,64);
[self.view addSubview:view2];
view2.backgroundColor=[UIColor yellowColor];
}
上面的显示是显而易见的。 但是当我们添加 在视图中手动添加 在添加 - (void)viewDidLoad {
[super viewDidLoad];
CGFloat width=self.view.frame.size.width;
CGFloat height=self.view.frame.size.height;
self.view.backgroundColor=[UIColor whiteColor];
UIView* view1=[[UIView alloc] init];
view1.frame=CGRectMake(0,100);
[self.view addSubview:view1];
view1.backgroundColor=[UIColor greenColor];
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,height) style:UITableViewStylePlain];
self.tableView.dataSource=self;
[self.view addSubview:self.tableView];
self.tableView.backgroundColor=[UIColor redColor];
}
显示效果如图: 但是,如果在添加 - (void)viewDidLoad {
[super viewDidLoad];
CGFloat width=self.view.frame.size.width;
CGFloat height=self.view.frame.size.height;
self.view.backgroundColor=[UIColor whiteColor];
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,height) style:UITableViewStylePlain];
self.tableView.dataSource=self;
[self.view addSubview:self.tableView];
self.tableView.backgroundColor=[UIColor redColor];
UIView* view1=[[UIView alloc] init];
view1.frame=CGRectMake(0,100);
[self.view addSubview:view1];
view1.backgroundColor=[UIColor greenColor];
}
注意:不管怎么设置,只是 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |