iOS屏幕尺寸适配 - 滚动视图

这里介绍一种屏幕适配方法,UIScrollView适配

层级介绍:
self.view{UIScrollView[UIView(子控件1,子控件2,子控件3......)]}

层级就是这样,将滚动视图加入到self.view中,再创建一个UIView(可作为contentView)添加到ScrollView中,子控件加入到contentView中

约束代码:

    UIScrollView *scrollV = [[UIScrollView alloc] init];
    scrollV.contentSize = CGSizeMake(kScreenW, kScreenH);
    scrollV.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.00];
    scrollV.bounces = NO;
    scrollV.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollV];

    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    [scrollV addSubview:contentView];
    self.contentView = contentView;
    
    UILabel *titleLb = [[UILabel alloc] init];
    titleLb.font = [UIFont systemFontOfSize:19];
    titleLb.numberOfLines = 0;
    titleLb.textColor = [UIColor blackColor];
    titleLb.textAlignment = NSTextAlignmentLeft;
    [contentView addSubview:titleLb];
    self.titleLb = titleLb;
    
    UILabel *authorLb = [[UILabel alloc] init];
    authorLb.font = [UIFont systemFontOfSize:14];
    authorLb.numberOfLines = 1;
    authorLb.textColor = [UIColor lightGrayColor];
    authorLb.textAlignment = NSTextAlignmentLeft;
    [contentView addSubview:authorLb];
    self.authorLb = authorLb;
    
    UILabel *timeLb = [[UILabel alloc] init];
    timeLb.font = [UIFont systemFontOfSize:14];
    timeLb.numberOfLines = 1;
    timeLb.textColor = [UIColor lightGrayColor];
    timeLb.textAlignment = NSTextAlignmentRight;
    [contentView addSubview:timeLb];
    self.timeLb = timeLb;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [contentView addSubview:imageView];
    self.iconView = imageView;
    
    UILabel *contentLb = [[UILabel alloc] init];
    contentLb.font = [UIFont systemFontOfSize:16];
    contentLb.numberOfLines = 0;
    contentLb.textColor = [UIColor darkGrayColor];
    [contentView addSubview:contentLb];
    self.contentLb = contentLb;


   [scrollV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollV);
        make.width.mas_equalTo(kScreenW);
    }];
    
    [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.top.equalTo(contentView).offset(15);
        make.right.equalTo(self.view).offset(-15);
    }];
    
    [authorLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(15);
        make.left.equalTo(self.view).offset(15);
        make.top.equalTo(titleLb.mas_bottom).offset(15);
        make.width.equalTo(contentView).multipliedBy(0.4);
    }];
    
    [timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(15);
        make.top.equalTo(titleLb.mas_bottom).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.width.equalTo(contentView).multipliedBy(0.4);
    }];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.top.equalTo(authorLb.mas_bottom).offset(12);
        make.height.mas_equalTo(155);
    }];
    
    [contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.top.equalTo(imageView.mas_bottom).offset(15);
        make.bottom.equalTo(contentView).offset(-20);
    }];

这里的话重点需要点一下的是,contentView不但需要上下左右设置好父类约束,还需要设置一下宽度约束,因为根据约束可知滚动视图的高度是由子控件撑起来的,但是宽度并没有被撑起来,所以我们需要固定contentView的宽度

约束的层级的话就是:
1.滚动视图对于self.view是上下左右等同
2.contentView对于滚动视图是上下左右等同,也必须固定宽度
3.最下面的子控件要设置好到contentView的距离,否则contentView不会被支撑起来,连带的滚动视图也不会支撑起来

效果:

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

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,294评论 4 61
  • 三件事 (1)阅读《必然》101~150页 (2)制作一个思维导图 (3)囚徒健身半小时 小确幸 老妈做的饭依旧是...
    咖飞的白阅读 108评论 0 0
  • 萧山花艺培训,20年花艺培训名校为您筑梦! 你说 你想成为一个懂生活的人 你想要学习一份工作之外的新技能 你梦想开...
    媞亚花语阅读 485评论 0 1
  • 最近一直想在思考关于项目的方法论,我所要叙述的方法论仅限于软件实施这个行业,小的软件产品可以,大的企业的CRM产品...
    童以默阅读 3,984评论 0 10