视图
把某个视图插入到指定位置,跟数组一样,位置都是从0开始算。若插的位置最顶层视图位置,那么这个视图就插在最顶层。
//
// ViewController.m
// 添加-视图
//
// Created by cqy on 16/2/12.
// Copyright © 2016年 程清杨. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 100)];
view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 100)];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 100)];
view2.backgroundColor = [UIColor greenColor];
[self.view addSubview:view2];
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(20, 70, 200, 100)];
view3.backgroundColor = [UIColor colorWithRed:200/255.0 green:30/255.0 blue:5/255.0 alpha:1];
[self.view addSubview:view3];
//把某个视图插入到最顶层
// [self.view insertSubview:<#(nonnull UIView *)#> aboveSubview:<#(nonnull UIView *)#>];
//把某个视图插入到最底层
//[self.view insertSubview:<#(nonnull UIView *)#> belowSubview:<#(nonnull UIView *)#>];
//插入到指定位置,这个位置是从0开始
//[self.view insertSubview:<#(nonnull UIView *)#> atIndex:<#(NSInteger)#>];
[self.view insertSubview:view atIndex:0];
[self.view insertSubview:view1 atIndex:1];
[self.view insertSubview:view2 atIndex:2];
//视图层次管理
//把指定的视图拿到最顶层
[self.view bringSubviewToFront:view];
//把指定视图拿到最底层
[self.view sendSubviewToBack:view];
//把某个视图与另一个视图交换位置
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
//把个视图移除
[view1 removeFromSuperview];
//视图属性
//视图隐藏:hidden,为BOOL值
view.hidden = YES;
//控制视图透明度:alpha,是一个CGFloat类型的变量,值在0~1之间
view3.alpha = 0.5;
// superview
// 获取⽗视图
UIView *superView = view.superview;
NSLog(@"--%@",[superView class]);
// subview
// 获取⼦视图
NSArray *subViews = self.view.subviews;
NSLog(@"==%@",subViews);
//tag通常
view.tag = 100;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// ViewController.m
// 添加-视图
//
// Created by cqy on 16/2/12.
// Copyright © 2016年 程清杨. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 100)];
view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 100)];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 100)];
view2.backgroundColor = [UIColor greenColor];
[self.view addSubview:view2];
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(20, 70, 200, 100)];
view3.backgroundColor = [UIColor colorWithRed:200/255.0 green:30/255.0 blue:5/255.0 alpha:1];
[self.view addSubview:view3];
//把某个视图插入到最顶层
// [self.view insertSubview:<#(nonnull UIView *)#> aboveSubview:<#(nonnull UIView *)#>];
//把某个视图插入到最底层
//[self.view insertSubview:<#(nonnull UIView *)#> belowSubview:<#(nonnull UIView *)#>];
//插入到指定位置,这个位置是从0开始
//[self.view insertSubview:<#(nonnull UIView *)#> atIndex:<#(NSInteger)#>];
[self.view insertSubview:view atIndex:0];
[self.view insertSubview:view1 atIndex:1];
[self.view insertSubview:view2 atIndex:2];
//视图层次管理
//把指定的视图拿到最顶层
[self.view bringSubviewToFront:view];
//把指定视图拿到最底层
[self.view sendSubviewToBack:view];
//把某个视图与另一个视图交换位置
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
//把个视图移除
[view1 removeFromSuperview];
//视图属性
//视图隐藏:hidden,为BOOL值
view.hidden = YES;
//控制视图透明度:alpha,是一个CGFloat类型的变量,值在0~1之间
view3.alpha = 0.5;
// superview
// 获取⽗视图
UIView *superView = view.superview;
NSLog(@"--%@",[superView class]);
// subview
// 获取⼦视图
NSArray *subViews = self.view.subviews;
NSLog(@"==%@",subViews);
//tag通常
view.tag = 100;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end