#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *myview=[[UIView alloc]init];
myview.frame=CGRectMake(40, 20, 200, 200);
myview.backgroundColor=[UIColor blueColor];
[self.view addSubview:myview];
NSLog(@"x:%f,t:%f,w:%f,h:%f.",myview.frame.origin.x,myview.frame.origin.y
,myview.frame.size.width,myview.frame.size.height);
NSLog(@"x:%f,t:%f,w:%f,h:%f.",myview.bounds.origin.x,myview.bounds.origin.y
,myview.bounds.size.width,myview.bounds.size.height);
NSLog(@"x:%f,t:%f.",myview.center.x,myview.center.y);
//父式图
UIView *superview=myview.superview;
superview.backgroundColor=[UIColor greenColor];
UIView *view2=[[UIView alloc]init];
view2.frame=CGRectMake(10, 20, 20, 20);
view2.backgroundColor=[UIColor yellowColor];
view2.tag=2;
[myview addSubview:view2];
UIView *view3=[[UIView alloc]init];
view3.frame=CGRectMake(5, 30, 50, 40);
view3.tag=3;
view3.backgroundColor=[UIColor purpleColor];
[myview addSubview:view3];
//将子视图加入数组中进行遍历
NSArray *mysubviews=myview.subviews;
for (UIView *view in mysubviews)
{
if (view.tag==2) {
view.backgroundColor=[UIColor greenColor];
}
view3.backgroundColor=[UIColor yellowColor];
}
//使用tag进行选择 viewwithtag
UIView *sub2view=[myview viewWithTag:3];
sub2view.backgroundColor=[UIColor grayColor];
//交换两个层式图
[myview exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
UIView *view4=[[UIView alloc]init];
view4.frame=CGRectMake(10, 20, 20, 20);
view4.backgroundColor=[UIColor blackColor];
//[myview insertSubview:view4 atIndex:2];
// [myview insertSubview:view4 belowSubview:view2];
//[myview bringSubviewToFront:view2];
[myview sendSubviewToBack:view3];
UIView *backview=[[UIView alloc]init];
backview.frame=CGRectMake([UIScreen mainScreen].bounds.size.width/2-25, 400, 50, 50);
backview.tag=1001;
backview.backgroundColor=[UIColor orangeColor];
[self.view addSubview:backview];
//准许子视图自适应
backview.autoresizesSubviews=YES;
UIView *topview=[[UIView alloc]init];
topview.frame=CGRectMake(10, 10, 30, 30);
topview.backgroundColor=[UIColor yellowColor];
//子视图自适应
topview.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
/* 跟随那一边进行变化
UIViewAutoresizingFlexibleLeftMargin;
UIViewAutoresizingFlexibleHeight;
UIViewAutoresizingFlexibleTopMargin;
*/
[backview addSubview:topview];
//button
UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
btn.frame=CGRectMake(10, 550, 355, 30);
btn.backgroundColor=[UIColor blackColor];
[btn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//button点击事件
-(void)btnclick{
UIView *view=[self.view viewWithTag:1001];
view.frame=CGRectMake(view.frame.origin.x-5, view.frame.origin.y-5, view.frame.size.width+10, view.frame.size.height+10);
}
@end
UIView基础
最新推荐文章于 2022-05-16 11:30:43 发布