代码创建 UI 组件时出现的错误 UILayoutConstraint translatesAutoresizingMaskIntoConstraints: Bool
来源:互联网 发布:java游戏编程入门pdf 编辑:程序博客网 时间:2024/06/05 02:16
本文主要是说 UIView 的一个属性值 translatesAutoresizingMaskIntoConstraints
: Bool
情况是这样的:我在学 UILayoutConstraint
,然后在一个 UIViewController
中代码新建一个 UIButton
,我把 button 添加到 view 中,设置其大小和约束,到执行的时候却没有按照约束的显示,反而在控制台出现约束错误信息。
代码:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 100))// 定义按钮外观button.layer.cornerRadius = 20button.layer.borderWidth = 1 / UIScreen.main.scalebutton.layer.borderColor = UIColor.lightGray.cgColor// 按钮状态button.titleLabel?.font = UIFont.systemFont(ofSize: 40)button.setTitleColor(UIColor.black, for: .normal)button.setTitleColor(UIColor.lightGray, for: .highlighted)button.setTitle("正常", for: UIControlState.normal)button.setTitle("高亮中", for: UIControlState.highlighted)view.addSubview(button)// 按钮约束button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = truebutton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = truebutton.heightAnchor.constraint(equalToConstant: 100).isActive = truebutton.widthAnchor.constraint(equalToConstant: 200).isActive = true
手机上的结果却是这样的:(看不清 右击在新窗口中打开图片)
控制台错误:(看不清 右击在新窗口中打开图片)
怎么弄都不对,我以为我写的约束有问题,困了我一整天时间,在静下心来查看错误的时候终于看到了答案。像错误信息中说的 注意 translatesAutoresizingMaskIntoConstraints
这个属性,如下图,然后查阅 API,得到的信息是,当用代码创建控件的时候,translatesAutoresizingMaskIntoConstraints
这个属性的值是 true,也就是说,在控件创建时,系统会自动根据控件的大小等生成关于这个控件的 约束,这就解释了为什么提示约束冲突,因为当你的 translatesAutoresizingMaskIntoConstraints
为 true 时,该控件已经有健全的约束了,你在代码中添加的那些自然会与已经存在的冲突。
API 信息 (看不清 右击在新窗口中打开图片)
在图形化编辑的界面中,控件的这个属性值是 false
,用代码生成的控件,这个属性值默认是 true
这个值来决定是否根据控件的大小自动生成约束。
知道为什么之后,修改代码:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 100))button.translatesAutoresizingMaskIntoConstraints = false // 添加的内容button.layer.cornerRadius = 20button.layer.borderWidth = 1 / UIScreen.main.scalebutton.layer.borderColor = UIColor.lightGray.cgColorbutton.titleLabel?.font = UIFont.systemFont(ofSize: 40)button.setTitleColor(UIColor.black, for: .normal)button.setTitleColor(UIColor.lightGray, for: .highlighted)button.setTitle("正常", for: UIControlState.normal)button.setTitle("高亮中", for: UIControlState.highlighted)view.addSubview(button)button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = truebutton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = truebutton.heightAnchor.constraint(equalToConstant: 100).isActive = truebutton.widthAnchor.constraint(equalToConstant: 200).isActive = true
得到结果:
关于 UILayoutConstraint 的知识看这里 http://blog.csdn.net/KimBing/article/details/78541677
- 代码创建 UI 组件时出现的错误 UILayoutConstraint translatesAutoresizingMaskIntoConstraints: Bool
- 代码添加constraint,设置translatesAutoresizingMaskIntoConstraints为NO的原因
- 代码添加constraint,设置translatesAutoresizingMaskIntoConstraints为NO的原因
- view的translatesAutoresizingMaskIntoConstraints属性
- Touchable类型的组件的子组件为复合类型时出现的错误解决
- 使用bool 类型变量赋值时的一个错误
- 在创建Android项目时出现appcompat_v7中的引用的组件出现No resource found
- XML接口对象创建时,链接出现错误的解决
- 解决WebService部署时出现的“未能创建类型...”错误
- 解决WebService部署时出现的“未能创建类型...”错误
- 解决WebService部署时出现的“未能创建类型...”错误
- 创建Windows身份验证的登录名时出现错误
- 创建矩阵输出最大值最小值时容易出现的错误
- Yii中创建Model时出现的错误…
- VS2013创建项目时出现脚本错误的解决方案
- 解决,从 IClassFactory 为 CLSID 为 的 COM 组件创建实例失败,原因是出现以下错误: 80004005
- 布局技巧:创建可重用的UI组件
- Android应用布局技巧:创建可复用的UI组件
- OpenGL中Camera类的设计以及使用
- Mac vmWare 找不到可以连接的有效对等进程
- 第一次使用maven创建web项目
- SQL 语法
- eclipse Clean 项目不生成class文件
- 代码创建 UI 组件时出现的错误 UILayoutConstraint translatesAutoresizingMaskIntoConstraints: Bool
- github restful api v3学习梳理
- Composer设置忽略版本匹配的方法
- linux进程管理
- LeetCode 328. Odd Even Linked List
- Mysql INT类型最大值探讨
- 登陆窗口进阶版
- sqlServer 更新一列为行号问题
- java运用itextpdf实现根据模板生成合同并返回网络路径,供用户查看