autolayout的使用原理及代码实现

2023-12-12 01:18

本文主要是介绍autolayout的使用原理及代码实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、
UIViewAutoresizing只能控制父子视图之间的布局
首先父视图要允许子视图自动伴随父视图变化而变化
然后子视图自己设置怎么伴随变化

设置 子视图 自动布局 1.必须父视图允许子视图自动布局 2.子视图自己设置布局方式


//父视图设置父子视图自适应/停靠模式
    _redView.autoresizesSubviews = YES;//允许子视图伴随父视图自动变化
//子视图设置子视图的自适应模式
    blueView.autoresizingMask


autolayout  iOS6的时候出来的  iPhone5出来了
sizeclass  iOS8的时候出来的

github

二.自动布局(多屏适配)
        1.为什么使用自动布局
            iphone手机的分辨率越来越多,有320*480,   640*960,  640*1136,  750*1334,  1080*1920
            使用代码控制frame坐标和大小的化,会花费大量时间精力.
            为了解决适配问题,苹果推出了自动布局

        2.自动布局原理
            通过给视图添加约束的方式,使视图在任何屏幕上都可以正常显示

        autolayout 用 constraints(约束) 来控制 控件的大小和位置

        auto layout 加约束条件 问题1:要么加少了 起不到作用,2.加多了产生了冲突 有可能崩溃

        3.如何给视图添加约束
            Xcode—>Editor—>Pin

        4.有哪些约束

                
            1).Width         //设置视图固定宽度
            2).Height        //设置视图固定高度
            
            3).Horizontal Spacing     //同级视图之间的横向间距
            4).Vertical Spacing        //同级视图之间的纵向间距
            
            5).Leading Space to SuperView       //与父视图的左间距
            6).Trailing Space to SuperView         //与父视图的右间距
            7).Top Space to SuperView              //与父视图的上间距
            8).Botton Space to SuperView        //与父视图的下间距

            9).Widths Equally                               //设置同级视图宽度比例
            10).Height Equally         //设置同级视图高度比例  

    NSLayoutConstraint *constraint =
    [NSLayoutConstraint
     constraintWithItem:self.contentView
              attribute:NSLayoutAttributeWidth
              relatedBy:NSLayoutRelationEqual
                 toItem:nil
              attribute:0
             multiplier:1.0
               constant:1000];
    
    NSLayoutConstraint *constraint2 =
    [NSLayoutConstraint
     constraintWithItem:self.contentView
     attribute:NSLayoutAttributeHeight
     relatedBy:NSLayoutRelationEqual
     toItem:nil
     attribute:0
     multiplier:1.0
     constant:200];
    
    [self.contentView addConstraint:constraint];
    [self.contentView addConstraint:constraint2];
    
    NSDictionary *views = NSDictionaryOfVariableBindings(_contentView);
    
//    NSDictionary *dict = @{@"_contentView": _contentView};
    
    NSArray *constraints =
    [NSLayoutConstraint constraintsWithVisualFormat:@"H:[_contentView(1000)]" options:0 metrics:nil views:views];
    NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[_contentView(200)]" options:0 metrics:nil views:views];
    
    [_contentView addConstraints:constraints];
    [_contentView addConstraints:constraints2];

===================================================
VFL 语言添加约束

UIView *redView = [[UIView alloc] initWithFrame:CGRectZero];
    redView.backgroundColor = [UIColor redColor];
    // 通常都设置成NO
    redView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:redView];
    
    UIView *blueView = [[UIView alloc] initWithFrame:CGRectZero];
    blueView.backgroundColor = [UIColor blueColor];
    // 通常都设置成NO
    blueView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:blueView];
    
    NSDictionary *views = NSDictionaryOfVariableBindings(redView, blueView);
    //NSDictionary *dict = @{@“redView”:redView,@“blueView”: blueView};


    NSArray *constraints =
    [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-100-[redView(100)]" options:0 metrics:nil views:views];
    [self.view addConstraints:constraints];
    
    NSArray *constraints2 =
    [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[redView(50)]" options:0 metrics:nil views:views];
    [self.view addConstraints:constraints2];
    
    NSArray *constraints3 =
    [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView]-50-[blueView(==redView)]" options:0 metrics:nil views:views];
    [self.view addConstraints:constraints3];
    
    NSArray *constraints4 =
    [NSLayoutConstraint constraintsWithVisualFormat:@"V:[redView]-50-[blueView(==redView)]" options:0 metrics:nil views:views];
    [self.view addConstraints:constraints4];



        5.用代码做自动布局
            
            使用第三方Masonry库

             [topView mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.top.equalTo(self.view.mas_top).with.offset(0);    //topView上边距与self.view上边距为0
                        make.left.equalTo(self.view.mas_left).with.offset(0);    //topView左边距与self.view左边距为0
                        make.right.equalTo(self.view.mas_right).with.offset(0);    //topView右边距与self.view右边距为0
                        make.height.equalTo(@64);                                            //topView宽度固定为64个像素
                    }];


                [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.top.equalTo(topView.mas_bottom).with.offset(0);        //tableView上边距与self.view下边距为0
                         make.bottom.equalTo(bottomView.mas_top).with.offset(0);    //tableView下边距与self.view上边距为0
                         make.left.equalTo(self.view.mas_left).with.offset(0);                //tableView左边距与self.view左边距为0
                         make.right.equalTo(self.view.mas_right).with.offset(0);            //tableView右边距与self.view右边距为0
                }];
//从下往上,从右往左,都是倒推,是负数.
UIButton *topButton  = [UIButton buttonWithType:UIButtonTypeSystem];
    topButton.backgroundColor = [UIColor grayColor];
    [topView addSubview:topButton];
    [topButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_top).with.offset(20);
        //从下往上,从右往左,都是倒推,是负数.
        make.bottom.equalTo(topView.mas_bottom).with.offset(-5);
        make.right.equalTo(topView.mas_right).with.offset(-20);
        make.width.equalTo(@100);
    }];            
[table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(0);
        make.right.equalTo(self.view.mas_right).with.offset(0);
        //table的上边距和topView的下边距为0
        make.top.equalTo(topView.mas_bottom).with.offset(0);
         //table的下边距和bottomView的上边距为0
        make.bottom.equalTo(bottomView.mas_top).with.offset(0);
    }];

这篇关于autolayout的使用原理及代码实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/482735

相关文章

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Spring @Scheduled注解及工作原理

《Spring@Scheduled注解及工作原理》Spring的@Scheduled注解用于标记定时任务,无需额外库,需配置@EnableScheduling,设置fixedRate、fixedDe... 目录1.@Scheduled注解定义2.配置 @Scheduled2.1 开启定时任务支持2.2 创建

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Conda虚拟环境的复制和迁移的四种方法实现

《Conda虚拟环境的复制和迁移的四种方法实现》本文主要介绍了Conda虚拟环境的复制和迁移的四种方法实现,包括requirements.txt,environment.yml,conda-pack,... 目录在本机复制Conda虚拟环境相同操作系统之间复制环境方法一:requirements.txt方法

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1