UIView基础



#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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值