1.说明
一个UIView里面可以包含多个Subview,Subview之间有阶层关系
新增 [Subview addSubview:Subview]
移除 [Subview removeFromSuperView]
往前移动一个图层
[UIView bringSubviewToFront:Subview]
往后移动一个图层
[UIView sendSubviewToBack:Subview]
使用索引Index交换两个Subview图层层级
[UIView exchangeSubviewAtIndex:indexA withSubviewAtIndex:indexB]
使用Subview变量名称取得在UIView中索引值
NSInteger index = [[UIVIew subviews] indexOfObject:Subview 名称]
2.实例
.h
#import <UIKit/UIKit.h>
@interface UIKitPrjSubviews :UIViewController
- (void)button11DidPush:(id)sender;
- (void)button111DidPush:(id)sender;
- (void)button112DidPush:(id)sender;
- (void)button1121DidPush:(id)sender;
- (void)button1122DidPush:(id)sender;
- (void)alertMessage:(UIButton*)button;
@end
.m
@interfaceUIKitPrjSubviews ()
@end
@implementation UIKitPrjSubviews
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorwhiteColor];
//追加1-1按钮
UIButton* button11 = [UIButtonbuttonWithType:UIButtonTypeSystem];
button11.frame =CGRectMake(10,10,300, 300);
[button11 setTitle:@"1-1"forState:UIControlStateNormal];
[button11 addTarget:selfaction:@selector(button11DidPush:)forControlEvents:UIControlEventTouchUpInside];
[button11.layersetBorderWidth:0.3];
[self.viewaddSubview:button11];
//追加1-1-1按钮
UIButton* button111 = [UIButtonbuttonWithType:UIButtonTypeSystem];
button111.frame =CGRectMake(20,20,260, 100);
[button111 setTitle:@"1-1-1"forState:UIControlStateNormal];
[button111 addTarget:selfaction:@selector(button111DidPush:)forControlEvents:UIControlEventTouchUpInside];
[button111.layersetBorderWidth:0.3];
[button11addSubview:button111];
//追加1-1-2按钮
UIButton* button112 = [UIButtonbuttonWithType:UIButtonTypeSystem];
button112.frame =CGRectMake(20,180,260, 100);
[button112 setTitle:@"1-1-2"forState:UIControlStateNormal];
[button112 addTarget:selfaction:@selector(button112DidPush:)forControlEvents:UIControlEventTouchUpInside];
[button112.layersetBorderWidth:0.3];
[button11addSubview:button112];
//追加1-1-2-1按钮
UIButton* button1121 = [UIButtonbuttonWithType:UIButtonTypeSystem];
button1121.frame =CGRectMake(10,10,95, 80);
[button1121 setTitle:@"1-1-2-1"forState:UIControlStateNormal];
[button1121 addTarget:selfaction:@selector(button1121DidPush:)forControlEvents:UIControlEventTouchUpInside];
[button1121.layersetBorderWidth:0.3];
[button112addSubview:button1121];
//追加1-1-2-2按钮
UIButton* button1122 = [UIButtonbuttonWithType:UIButtonTypeSystem];
button1122.frame =CGRectMake(155,10,95, 80);
[button1122 setTitle:@"1-1-2-2"forState:UIControlStateNormal];
[button1122 addTarget:selfaction:@selector(button1122DidPush:)forControlEvents:UIControlEventTouchUpInside];
[button1122.layersetBorderWidth:0.3];
[button112addSubview:button1122];
}
- (void)button11DidPush:(id)sender {
[selfalertMessage:sender];
}
- (void)button111DidPush:(id)sender {
[selfalertMessage:sender];
}
- (void)button112DidPush:(id)sender {
[selfalertMessage:sender];
}
- (void)button1121DidPush:(id)sender {
[selfalertMessage:sender];
}
- (void)button1122DidPush:(id)sender {
[selfalertMessage:sender];
}
- (void)alertMessage:(UIButton *)button {
//显示self的标题作为警告框的标题
NSString* title = [NSStringstringWithFormat:@"self = %@",button.titleLabel.text];
//取得superview的标题
//当superview非UIButton的情况下,以UIViewController代替
NSString* superViewName;
if ([button.superviewisKindOfClass:[UIButtonclass]]) {
superViewName = ((UIButton*)button.superview).titleLabel.text;
}else {
superViewName =@"UIViewController";
}
//取得subviews的标题
NSMutableString* subviews = [[NSMutableStringalloc]initWithCapacity:64];//最大容量64
[subviewssetString:@""];
for (id viewin button.subviews) {
NSString* addString;
if ([viewisKindOfClass:[UIButtonclass]]) {
addString = ((UIButton*)view).titleLabel.text;
}elseif ([viewisKindOfClass:[UILabelclass]]) {
addString = ((UILabel*)view).text;
}else {
addString = [viewdescription];
}
if ([subviewslength] >0) {
[subviewsappendString:@","];
}
}
NSString* message = [NSStringstringWithFormat:@"superview = %@\r\nsubviews = %@",superViewName,subviews];
UIAlertView* alert = [[UIAlertViewalloc]initWithTitle:titlemessage:messagedelegate:nilcancelButtonTitle:nilotherButtonTitles:@"OK",nil];
[alertshow];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self.navigationControllersetNavigationBarHidden:NOanimated:YES];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end