添加UIView
center:获得中心点(CGRectGetMidx 获取当前frame中心点的x)
bounds:设置大小
orangeView.clipsToBounds;把超出父视图的部分清除
如果要设置视图的圆角,边框,阴影都必须在layer层进行
视图层次的管理
1.交换视图的位置
直接交换某两个视图的位置
[self.view exchangeSubviewAtIndex:100 withSubviewAtIndex:101];//100.101 是视图的tag值
把某个视图放到最前面
[self.view bringSubviewToFront:orangeView];
把某个视图放在最后面
[self.view sendSubviewToBack:blackView];
2.插入视图:默认插入最上面
将某个视图插入到指定视图的上方
[self.view insertSubview:purpleView aboveSubview:orangeView];
将某个视图插入到指定视图的下方
[self.view insertSubview:purpleView belowSubview:blackView];
3.移除子视图获取所有子视图
NSArray * array = self.view.subviews;
移除单个子视图[blackView removeFromSuperview];
移除所有子视图:遍历数组
for (UIView *view in array) {
[view removeFromSuperview];
}
推送下一个页面
RootViewController.m
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 500, 70, 40);
[button setTitle:@"next" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 100;
[self.view addSubview:button];
- (void)buttonPressed:(UIButton *)sender{
DetailViewController *detailVC = [[DetailViewController alloc] init];
[self presentViewController:detailVC animated:YES completion:nil];
/*
参数1:需要跳转到哪个控制器
参数2:是否执行动画
参数3:代码块,界面跳转结束后做什么
*/
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.bounds = CGRectMake(0, 0, 100, 40);
button.center = CGPointMake(CGRectGetMidX(self.view.bounds), 500);
[button setTitle:@"上一页" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
//button.backgroundColor = [UIColor redColor];
button.tag = 10;
[self.view addSubview:button];
//页面返回去
- (void)buttonPressed:(UIButton *)sender{
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
当DetailViewController返回到RootViewController时,DetailViewController将会被销毁.