代码实例(包括层级关系,这两段都只是ViewController.m)
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView* view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 200);
view.backgroundColor = [UIColor greenColor];
[self.view addSubview:view];
view.hidden = NO;
view.alpha = 0.5;
self.view.backgroundColor = [UIColor blueColor];
view.opaque = YES;
[view removeFromSuperview];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView* view01 = [[UIView alloc] init];
view01.frame = CGRectMake(100, 100, 150, 150);
view01.backgroundColor = [UIColor blueColor];
UIView* view02 = [[UIView alloc] init];
view02.frame = CGRectMake(125, 125, 150, 150);
view02.backgroundColor = [UIColor orangeColor];
UIView* view03 = [[UIView alloc] init];
view03.frame = CGRectMake(150, 150, 150, 150);
view03.backgroundColor = [UIColor greenColor];
[self.view addSubview:view01];
[self.view addSubview:view02];
[self.view addSubview:view03];
[self.view bringSubviewToFront:view03];
[self.view sendSubviewToBack:view02];
UIView* viewFront = self.view.subviews[2];
UIView* viewBack = self.view.subviews[0];
if (viewBack == view02) {
NSLog(@"相等");
}
[view01 removeFromSuperview];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
心得体会
- 讲了三种从屏幕上让UIView消失的方法
- 透明度
- 是否隐藏
- 移去
- 注意的是第三种是真正移去,也就是连触发事件都不能,而前两种都只是看不见
- 层级关系就两句话
- 先出现的在下面
- subviews数组的顺序是重叠顺序,最下面的第一个