Swift - 自动布局库SnapKit的使用详解4(样例1:实现一个登录页面)
SnapKit文章系列:(代码均已升级至Swift3)
Swift - 自动布局库SnapKit的使用详解1(配置、使用方法、样例)
Swift - 自动布局库SnapKit的使用详解2(约束的更新、移除、重做)
Swift - 自动布局库SnapKit的使用详解3(约束优先级,约束做动画)
Swift - 自动布局库SnapKit的使用详解4(样例1:实现一个登录页面) --- 当前文章
Swift - 自动布局库SnapKit的使用详解5(样例2:实现一个计算器界面)
Swift - 自动布局库SnapKit的使用详解1(配置、使用方法、样例)
Swift - 自动布局库SnapKit的使用详解2(约束的更新、移除、重做)
Swift - 自动布局库SnapKit的使用详解3(约束优先级,约束做动画)
Swift - 自动布局库SnapKit的使用详解4(样例1:实现一个登录页面) --- 当前文章
Swift - 自动布局库SnapKit的使用详解5(样例2:实现一个计算器界面)
前面的几篇文章讲解了自动布局库SnapKit的使用方法。本文通过一个完整的样例(登录页面)来演示在实际项目中如何使用SnapKit来实现自动化布局的。
1,效果图如下
2,代码讲解
(1)用户名、密码输入区域(白色区域)设置垂直居中约束,其高度是固定90,宽度自适应(距离屏幕左右侧都为15像素)
(2)用户名、密码输入框之间分割线是使用灰色背景的UIView实现,其高度是1像素,同样设置垂直居中约束。
(3)登录按钮距离上方输入区域20像素,高度固定是40,宽度同样自适应(距离屏幕左右侧都为15像素)
(4)上方标题标签据下方输入区域20像素,宽度自适应,内容居中。
(5)当键盘出现时,修改输入区域的垂直约束偏移量,使其向上移动(移动时,登陆按钮、标题标签也会同步移动)。避免在使用小屏设备时,键盘把登录框给挡住。
(6)键盘消失时,整个登录区域又会下移回复原位(上移、下移过程都有动画效果)
3,页面代码
import UIKit import SnapKit class ViewController: UIViewController, UITextFieldDelegate { var txtUser: UITextField! //用户名输入框 var txtPwd: UITextField! //密码输入款 var formView: UIView! //登陆框视图 var horizontalLine: UIView! //分隔线 var confirmButton:UIButton! //登录按钮 var titleLabel: UILabel! //标题标签 var topConstraint: Constraint? //登录框距顶部距离约束 override func viewDidLoad() { super.viewDidLoad() //视图背景色 self.view.backgroundColor = UIColor(red: 1/255, green: 170/255, blue: 235/255, alpha: 1) //登录框高度 let formViewHeight = 90 //登录框背景 self.formView = UIView() self.formView.layer.borderWidth = 0.5 self.formView.layer.borderColor = UIColor.lightGray.cgColor self.formView.backgroundColor = UIColor.white self.formView.layer.cornerRadius = 5 self.view.addSubview(self.formView) //最常规的设置模式 self.formView.snp.makeConstraints { (make) -> Void in make.left.equalTo(15) make.right.equalTo(-15) //存储top属性 self.topConstraint = make.centerY.equalTo(self.view).constraint make.height.equalTo(formViewHeight) } //分隔线 self.horizontalLine = UIView() self.horizontalLine.backgroundColor = UIColor.lightGray self.formView.addSubview(self.horizontalLine) self.horizontalLine.snp.makeConstraints { (make) -> Void in make.height.equalTo(0.5) make.left.equalTo(15) make.right.equalTo(-15) make.centerY.equalTo(self.formView) } //用户名图标 let imgLock1 = UIImageView(frame:CGRect(x: 11, y: 11, width: 22, height: 22)) imgLock1.image = UIImage(named:"iconfont-user") //密码图标 let imgLock2 = UIImageView(frame:CGRect(x: 11, y: 11, width: 22, height: 22)) imgLock2.image = UIImage(named:"iconfont-password") //用户名输入框 self.txtUser = UITextField() self.txtUser.delegate = self self.txtUser.placeholder = "用户名" self.txtUser.tag = 100 self.txtUser.leftView = UIView(frame:CGRect(x: 0, y: 0, width: 44, height: 44)) self.txtUser.leftViewMode = UITextFieldViewMode.always self.txtUser.returnKeyType = UIReturnKeyType.next //用户名输入框左侧图标 self.txtUser.leftView!.addSubview(imgLock1) self.formView.addSubview(self.txtUser) //布局 self.txtUser.snp.makeConstraints { (make) -> Void in make.left.equalTo(15) make.right.equalTo(-15) make.height.equalTo(44) make.centerY.equalTo(self.formView).offset(-formViewHeight/4) } //密码输入框 self.txtPwd = UITextField() self.txtPwd.delegate = self self.txtPwd.placeholder = "密码" self.txtPwd.tag = 101 self.txtPwd.leftView = UIView(frame:CGRect(x: 0, y: 0, width: 44, height: 44)) self.txtPwd.leftViewMode = UITextFieldViewMode.always self.txtPwd.returnKeyType = UIReturnKeyType.next //密码输入框左侧图标 self.txtPwd.leftView!.addSubview(imgLock2) self.formView.addSubview(self.txtPwd) //布局 self.txtPwd.snp.makeConstraints { (make) -> Void in make.left.equalTo(15) make.right.equalTo(-15) make.height.equalTo(44) make.centerY.equalTo(self.formView).offset(formViewHeight/4) } //登录按钮 self.confirmButton = UIButton() self.confirmButton.setTitle("登录", for: UIControlState()) self.confirmButton.setTitleColor(UIColor.black, for: UIControlState()) self.confirmButton.layer.cornerRadius = 5 self.confirmButton.backgroundColor = UIColor(colorLiteralRed: 1, green: 1, blue: 1, alpha: 0.5) self.confirmButton.addTarget(self, action: #selector(loginConfrim), for: .touchUpInside) self.view.addSubview(self.confirmButton) self.confirmButton.snp.makeConstraints { (make) -> Void in make.left.equalTo(15) make.top.equalTo(self.formView.snp.bottom).offset(20) make.right.equalTo(-15) make.height.equalTo(44) } //标题label self.titleLabel = UILabel() self.titleLabel.text = "hangge.com" self.titleLabel.textColor = UIColor.white self.titleLabel.font = UIFont.systemFont(ofSize: 36) self.view.addSubview(self.titleLabel) self.titleLabel.snp.makeConstraints { (make) -> Void in make.bottom.equalTo(self.formView.snp.top).offset(-20) make.centerX.equalTo(self.view) make.height.equalTo(44) } } //输入框获取焦点开始编辑 func textFieldDidBeginEditing(_ textField:UITextField) { UIView.animate(withDuration: 0.5, animations: { () -> Void in self.topConstraint?.update(offset: -125) self.view.layoutIfNeeded() }) } //输入框返回时操作 func textFieldShouldReturn(_ textField:UITextField) -> Bool { let tag = textField.tag switch tag { case 100: self.txtPwd.becomeFirstResponder() case 101: loginConfrim() default: print(textField.text!) } return true } //登录按钮点击 func loginConfrim(){ //收起键盘 self.view.endEditing(true) //视图约束恢复初始设置 UIView.animate(withDuration: 0.5, animations: { () -> Void in self.topConstraint?.update(offset: 0) self.view.layoutIfNeeded() }) } }源码下载:hangge_1112.zip
xcode8.2.1 ,下载最新版的SnapKit,编译不通过。
SnapKit的约束好像都是单个视图进行约束
我以前习惯VFL,请问SnapKit有没有办法作同时对多个视图设置约束,类似这样:
V:|-10-[_topLabel(35)]-10-[_line(1)]-10-[_bottomLabel(35)]-10-|",这样更能从宏观上把握整个界面
航哥,Demo源码下载下来是报错的。