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

相关文章

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Pydantic中Optional 和Union类型的使用

《Pydantic中Optional和Union类型的使用》本文主要介绍了Pydantic中Optional和Union类型的使用,这两者在处理可选字段和多类型字段时尤为重要,文中通过示例代码介绍的... 目录简介Optional 类型Union 类型Optional 和 Union 的组合总结简介Pyd

Pydantic中model_validator的实现

《Pydantic中model_validator的实现》本文主要介绍了Pydantic中model_validator的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录引言基础知识创建 Pydantic 模型使用 model_validator 装饰器高级用法mo

Vue3使用router,params传参为空问题

《Vue3使用router,params传参为空问题》:本文主要介绍Vue3使用router,params传参为空问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录vue3使用China编程router,params传参为空1.使用query方式传参2.使用 Histo

AJAX请求上传下载进度监控实现方式

《AJAX请求上传下载进度监控实现方式》在日常Web开发中,AJAX(AsynchronousJavaScriptandXML)被广泛用于异步请求数据,而无需刷新整个页面,:本文主要介绍AJAX请... 目录1. 前言2. 基于XMLHttpRequest的进度监控2.1 基础版文件上传监控2.2 增强版多

使用Python自建轻量级的HTTP调试工具

《使用Python自建轻量级的HTTP调试工具》这篇文章主要为大家详细介绍了如何使用Python自建一个轻量级的HTTP调试工具,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录一、为什么需要自建工具二、核心功能设计三、技术选型四、分步实现五、进阶优化技巧六、使用示例七、性能对比八、扩展方向建

Redis分片集群的实现

《Redis分片集群的实现》Redis分片集群是一种将Redis数据库分散到多个节点上的方式,以提供更高的性能和可伸缩性,本文主要介绍了Redis分片集群的实现,具有一定的参考价值,感兴趣的可以了解一... 目录1. Redis Cluster的核心概念哈希槽(Hash Slots)主从复制与故障转移2.

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.