博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SnapKit自动布局(三)
阅读量:7091 次
发布时间:2019-06-28

本文共 1494 字,大约阅读时间需要 4 分钟。

也许你在写OC的时候已经用过了Masonry这个第三方库来写自动布局,今天我们来说说Swift版本的Masonry第三方库SnapKit

snp_remakeConstraints

snp_remakeConstraints is similar to snp_makeConstraints, but will first remove all existing constraints installed by SnapKit.

移除之前的所有约束,然后添加新约束的方法是:snp_makeConstraints

我们来体验一下 snp_makeConstraints这个方法。

效果图:

Show Your Code

初始化 button

var button: UIButton!    var isExpanded = false        button = UIButton(type: UIButtonType.System)       button.setTitle("点击展开", forState: .Normal)       button.layer.borderWidth = 3       button.layer.borderColor = UIColor.redColor().CGColor       button.backgroundColor = UIColor.greenColor()       button.addTarget(self, action: "change", forControlEvents: UIControlEvents.TouchUpInside)       view.addSubview(button)

重写方法

override func updateViewConstraints() {// 这里使用update也是一样的。// remake会将之前的全部移除,然后重新添加      button.snp_remakeConstraints { (make) -> Void in          make.top.leading.trailing.equalTo(view)          if isExpanded {              make.bottom.equalTo(view)          }else {              make.bottom.equalTo(-300)          }      }      super.updateViewConstraints()  }

按钮方法

func change() {      isExpanded = !isExpanded      if !isExpanded {          button.setTitle("点击展开", forState: .Normal)      }else {          button.setTitle("点击收起", forState: .Normal)      }      self.view.setNeedsUpdateConstraints()      self.view.updateConstraintsIfNeeded()      UIView.animateWithDuration(0.5) { () -> Void in          self.view.layoutIfNeeded()      }  }

转载地址:http://soiql.baihongyu.com/

你可能感兴趣的文章
我身边的一些数据库事故
查看>>
SAP与Apple合作再升级,将推出SAP Cloud Platform SDK for iOS
查看>>
C#如何把List of Object转换成List of T具体类型
查看>>
java8中的localdate和localtime用法举例
查看>>
[20160713]修改表结构增加1列与缺省值.txt
查看>>
Nginx工作原理和优化、漏洞(转)
查看>>
8天学通MongoDB——第四天 索引操作
查看>>
让c#的exe只要被修改就无法运行,支持混淆和数字证书
查看>>
导入https证书
查看>>
struct和typedef struct
查看>>
手机分发真能抢媒体平台的饭吗?
查看>>
大叔也说Xamarin~Android篇~支付宝SDK的集成
查看>>
RestServer 2.0 正式版发布
查看>>
白板编程浅谈——Why, What, How(转)
查看>>
http协议的MP4文件播放问题的分析
查看>>
ObjC学习(2):数据类型(1)
查看>>
深入浅出如何解析xml文件---上篇
查看>>
activemq安装
查看>>
在Win7下用XManager远程控制ubuntu
查看>>
Linux下用gSOAP开发Web Service服务端和客户端程序(一)
查看>>