5月31日-Autoresizing

屏幕适配的发展历史

iPhone3GS\iPhone4

  • 没有屏幕适配可言
  • 全部用frame、bounds、center进行布局
  • 很多这样的现象:坐标值、宽度高度值全部写死
    UIButton *btn1 = [[UIButton alloc] init];
    btn1.frame = CGRectMake(0, 0, 320 - b, 480 - c);
    

iPad出现、iPhone横屏

  • 出现Autoresizing技术
    • 让横竖屏适配相对简单
    • 让子控件可以跟随父控件的行为自动发生相应的变化
    • 前提是:关闭Autolayout功能
    • 局限性
      • 只能解决子控件跟父控件的相对关系问题
      • 不能解决兄弟控件的相对关系问题

 UIViewAutoresizingNone                 = 0,
 UIViewAutoresizingFlexibleLeftMargin   = 1 << 0, 距离父控件左边的间距是伸缩的(不固定的)
 UIViewAutoresizingFlexibleRightMargin  = 1 << 2, 距离父控件右边的间距是伸缩的(不固定的)
 UIViewAutoresizingFlexibleTopMargin    = 1 << 3, 距离父控件顶部的间距是伸缩的(不固定的)
 UIViewAutoresizingFlexibleBottomMargin = 1 << 5, 距离父控件底部的间距是伸缩的(不固定的)
 UIViewAutoresizingFlexibleWidth        = 1 << 1, 宽度跟随父控件的宽度进行自动伸缩
 UIViewAutoresizingFlexibleHeight       = 1 << 4, 高度跟随父控件的高度进行自动伸缩

演示代码:

#import "ViewController.h"

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

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 创建蓝色UIView
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    blueView.frame = CGRectMake(0, 0, 250, 250);
    [self.view addSubview:blueView];
    self.blueView = blueView;
    // 创建红色UIView
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    redView.frame = CGRectMake(0, 150, 250, 100);
    redView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
    [blueView addSubview:redView];
}
// 点击View就会自动调用
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 在100范围内随机产生值
    CGFloat w = 200 + arc4random_uniform(100);
    CGFloat h = 200 + arc4random_uniform(100);
    self.blueView.frame = CGRectMake(0, 0, w, h);
}

iOS 6.0(Xcode4)开始

  • 出现了Autolayout技术
  • 从Xcode5.0(iOS 7.0)开始,开始流行Autolayout

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 黑夜忽然变得很漫长仿佛是一生一辈子那样总也走不完除了心脏孤独跳动的声音世界犹如在静止的蛮荒思绪被这份沉重压迫无助与...
    丁_香阅读 690评论 38 88
  • 天雨虽大,不润无根之草,佛法虽宽,不渡无缘之人。机会都是从相信和行动开始,我的机会来了,一定要加油把握。
    吕明超阅读 91评论 0 0