IOS学习 autoresizing 屏幕适配 父子视图

本文介绍了一个iOS应用中的视图自适应(autoresizing)实现方式,并演示了如何处理触摸事件(touchesBegan)来改变视图尺寸。通过设置UIView的autoresizingMask属性,使子视图能够根据父视图的变化进行自适应调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@interface HomeViewController ()


@property (nonatomic,retain)UIView *redView;


@end


@implementation HomeViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    _redView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

    _redView.backgroundColor = [UIColor redColor];

    [self.view addSubview:_redView];

    

    UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];

    blueView.backgroundColor = [UIColor blueColor];

    [_redView addSubview:blueView];


    //设置blueView属性

    /*   

     外部四根线勾选

     UIViewAutoresizingNone                 = 0,

     左侧灵活,外部右侧线勾选

     UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,

     宽度灵活,内部横向线勾选

     UIViewAutoresizingFlexibleWidth        = 1 << 1,

     右侧灵活,外部左侧线勾选

     UIViewAutoresizingFlexibleRightMargin  = 1 << 2,

     上部灵活,外部下侧线勾选

     UIViewAutoresizingFlexibleTopMargin    = 1 << 3,

     高度灵活,内部纵向线勾选

     UIViewAutoresizingFlexibleHeight       = 1 << 4,

     底部灵活,外部上侧线勾选

     UIViewAutoresizingFlexibleBottomMargin = 1 << 5

     */

    blueView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

   /*

    autoresizing  只能参照父类来设置  (了解)

    autolayout 是互斥的,不能并存

    */

}


//点击触发事件

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    CGRect redbounds = _redView.bounds;

    redbounds.size.width += 20;

    redbounds.size.height += 20;

    

    _redView.bounds = redbounds;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值