iOS 停靠模式

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView * view1=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    view1.backgroundColor=[UIColor orangeColor];
    view1.tag=10;
    [self.view addSubview:view1];

    //激活停靠模式
    //view1.autoresizesSubviews=YES;
    //设置停靠模式的参数


    UIView * view2=[[UIView alloc]initWithFrame:CGRectMake(20, 20, 60, 60)];
    view2.backgroundColor=[UIColor blueColor];
    [view1 addSubview:view2];
    //激活停靠模式
    view2.autoresizesSubviews=YES;
    //设置停靠模式的参数
    //UIViewAutoresizingFlexibleLeftMargin 停靠在右边,距离父视图右边距离不变化
    //UIViewAutoresizingFlexibleWidth  宽度自适应,随着父试图的变化而变化
    [view2 setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

    UIButton * btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(50, 400, 200, 100);
    [btn setTitle:@"停靠模式变大" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)btnAction:(UIButton*)sender
{
    //用标记获取view1的tag值
    UIView * view1=[self.view viewWithTag:10];
    //获取当前view1的大小
    view1.frame=CGRectMake(view1.frame.origin.x, view1.frame.origin.y, view1.frame.size.width+10, view1.frame.size.height+10);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值