屏幕适配之-Autoresizing

本文介绍了Autoresizing技术,一种用于组件定位的技术。文章详细说明了Autoresizing的使用限制及其与Autolayout的区别,并提供了具体的实现代码示例。

1.对autoresizing的简单介绍

autoresizing是对组件进行定位的一种技术。它不能和autolayout进行同时使用。并且在使用autoresizing的时候,只能控制父子视图之间的关系。

2.具体用法

第一种是在IB中,在Size inspector中进行设置;第二种方式是用代码的方法。下面是利用代码方法的例子:

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,strong) UIView *blueView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    self.blueView = blueView;



    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 250, 100)];
    redView.backgroundColor = [UIColor redColor];
    redView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
    [blueView addSubview:redView];



}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGFloat w = 200 + arc4random_uniform(100);
    CGFloat h = 200 + arc4random_uniform(100);
    self.blueView.frame = CGRectMake(0, 0, w, h);
}

- (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、付费专栏及课程。

余额充值