swift纯代码实现自动布局

发布时间:2022-06-26 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了swift纯代码实现自动布局脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

早期的IOS设备都是固定屏幕的尺寸,所以大家写iOS应用几乎都是用frame布局,直接写死宽高和坐标。但是iPhone6开始,iOS设备出现了多种尺寸,于是苹果推出了一种新的布局方式:`autolayout`

autolaout又名自动布局,或者是是相对布局。使用autolayout可以轻易写出目前主流的所有页面布局。

关闭autoresizing布局方式

在 iOS中,默认使用aotoresizing的布局方式来写页面,autoresizing相对来说比较简单。只做了相对于父控件的大小的伸缩。使用起来不是特别顺手。控件的translatesAutoresizingMaskIntoConstraints为false,可以启用自动布局

NSLayoutConstraint简单了解

顾名思义,这是创建一个布局约束

NSLayoutConstraint(item: 当前的控件, attribute: 约束属性, relatedBy: 前后之间的关系, toItem: 相对的控件, attribute: 相对控件的某个属性, multiplier: 比例, constant: 常量)

比如

NSLayoutConstraint(item:myview , attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: 0)

意思就是myview左边相对于self.view的左边,距离为0

创建一个与ViewController一样大的View

 let myview = UIView();
        self.view.addSubview(myview)
        myview.translatesAutoresizingMaskIntoConstraints = false
        
        myview.backgroundColor = UIColor.systemBlue
        let left = NSLayoutConstraint(item:myview , attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: 0)
        let right = NSLayoutConstraint(item: myview
                                       , attribute: .right, relatedBy: .equal, toItem: self.view
                                       , attribute: .right, multiplier: 1, constant: 0)
        let top = NSLayoutConstraint(item: myview
                                     , attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0)
        let bottom = NSLayoutConstraint(item: myview
                                        , attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: 0)
        myview.superview?.addConstraints([left,right,top,bottom])

swift纯代码实现自动布局

约束某个某个属性为固定值

比如我们需要设置当前view的宽度为100.高度为200

let myview = UIView();
        self.view.addSubview(myview)
        myview.translatesAutoresizingMaskIntoConstraints = false
        
        myview.backgroundColor = UIColor.systemBlue
        let left = NSLayoutConstraint(item:myview , attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: 0)
        let width = NSLayoutConstraint(item: myview
                                       , attribute: .width, relatedBy: .equal, toItem: nil
                                       , attribute: .notAnAttribute, multiplier: 1, constant: 100)
        let top = NSLayoutConstraint(item: myview
                                     , attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0)
        let height = NSLayoutConstraint(item: myview
                                        , attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200)
        myview.superview?.addConstraints([left,width,top,height])

swift纯代码实现自动布局

View居中

 let myview = UIView();
        self.view.addSubview(myview)
        myview.translatesAutoresizingMaskIntoConstraints = false
        
        myview.backgroundColor = UIColor.systemBlue
        let centerX = NSLayoutConstraint(item:myview , attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0)
        let width = NSLayoutConstraint(item: myview
                                       , attribute: .width, relatedBy: .equal, toItem: nil
                                       , attribute: .notAnAttribute, multiplier: 1, constant: 100)
        let centerY = NSLayoutConstraint(item: myview
                                     , attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1, constant: 0)
        let height = NSLayoutConstraint(item: myview
                                        , attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200)
        myview.superview?.addConstraints([centerX,width,centerY,height])

swift纯代码实现自动布局

View动画

 

脚本宝典总结

以上是脚本宝典为你收集整理的swift纯代码实现自动布局全部内容,希望文章能够帮你解决swift纯代码实现自动布局所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: