不明原因的约束报错的两种处理方式
相信大家在iOS的开发中,经常会遇到一些不明原因的约束警告,有时候按百度到的方法试一下就好了,有时候却不行.而且下一次可能还会出现,比如下面这种约束的报错
Unable to simultaneously satisfy constraints.Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
("<NSLayoutConstraint:0x11d748d0 V:[UITextView:0xc1bb000(65)]>","<NSLayoutConstraint:0x11d77620 V:[UIButton:0x11d71cf0(44)]>","<NSLayoutConstraint:0x11d7be30 V:[UIButton:0x11d79e70(43)]>","<NSLayoutConstraint:0xa1980d0 V:|-(134)-[UITextView:0xc1bb000] (Names: '|':UIView:0xa1afba0 )>","<NSLayoutConstraint:0xa199ed0 UITextView:0xc1bb000.centerY == UIButton:0x11d71cf0.centerY>","<NSLayoutConstraint:0xa199e50 V:[UIButton:0x11d79e70]-(61)-[UIButton:0x11d71cf0]>","<NSLayoutConstraint:0xa199cb0 V:|-(40)-[UIButton:0x11d79e70] (Names: '|':UIView:0xa1afba0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x11d748d0 V:[UITextView:0xc1bb000(65)]>
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
复制代码
一般来说,出现这种警告有两种情况:
- 使用xib中自带的view,对其中子控件添加了Autolayout约束,在调用xib后又调整了view的frame
- 纯代码方式使用Autolayout,但没有对使用的View的translatesAutoresizingMaskIntoConstraints的属性设置为NO.
- 如果是从代码层面开始使用Autolayout,需要对使用的View的translatesAutoresizingMaskIntoConstraints的属性设置为NO. 即可开始通过代码添加Constraint,否则View还是会按照以往的autoresizingMask进行计算. 而在Interface Builder中默认勾选了Ues Autolayout,IB生成的控件的translatesAutoresizingMaskIntoConstraints属性都会被默认设置NO.
UIView *redView = [[UIView alloc] init];redView.backgroundColor= [UIColorredColor];//禁止autoresizing自动转换为约束(特别重要)redView.translatesAutoresizingMaskIntoConstraints= NO;[self.view addSubview:redView];//添加约束//高度约束NSLayoutConstraint*hlc = [NSLayoutConstraintconstraintWithItem:redView attribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeNotAnAttributemultiplier:0.0constant:100];[redViewaddConstraint:hlc];//宽度约束NSLayoutConstraint*wlc = [NSLayoutConstraintconstraintWithItem:redView attribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeNotAnAttributemultiplier:0.0constant:100];[redViewaddConstraint:wlc];//右边间距约束NSLayoutConstraint*rightlc = [NSLayoutConstraintconstraintWithItem:redView attribute:NSLayoutAttributeRightrelatedBy:NSLayoutRelationEqualtoItem:self.viewattribute:NSLayoutAttributeRightmultiplier:1.0constant:-20];[self.view addConstraint:rightlc];//底部间距约束NSLayoutConstraint*bottomlc = [NSLayoutConstraintconstraintWithItem:redView attribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:self.viewattribute:NSLayoutAttributeBottommultiplier:1.0constant:-20];[self.view addConstraint:bottomlc];
复制代码
- 另一种是通过xib的方式也有可能会出现这个警告,这是因为view的autoresizingMask属性引起的
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {UIViewAutoresizingNone = 0,UIViewAutoresizingFlexibleLeftMargin = 1 << 0,UIViewAutoresizingFlexibleWidth = 1 << 1,UIViewAutoresizingFlexibleRightMargin = 1 << 2,UIViewAutoresizingFlexibleTopMargin = 1 << 3,UIViewAutoresizingFlexibleHeight = 1 << 4,UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
复制代码
一般情况下,以下这些view的autoresizingMask值默认就是18(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth
)
1.从xib里面创建出来的默认控件(Xcode自动创建出来的那个view) 2.控制器的view
如果不希望控件拥有autoresizingMask的自动伸缩功能,应该设置为none.如果从xib中加载自带的view出现约束警告,也应该将其设置为none
blueView.autoresizingMask = UIViewAutoresizingNone;
这样烦人的约束警告就不会再出现了,关于这两个属性的详细解释,可以查看下面两个链接中的两篇文章.
不明原因的约束报错的两种处理方式相关推荐
- 继承WebMvcConfigurer 和 WebMvcConfigurerAdapter类依然CORS报错? springboot 两种方式稳定解决跨域问题
继承WebMvcConfigurer 和 WebMvcConfigurerAdapter类依然CORS报错???springboot 两种方式稳定解决跨域问题! 之前我写了一篇文章,来解决CORS报错 ...
- Springboot pox.xml 报错解决两种方式
首次运用spring boot,然后细心的学习了一天,每一个系统在没有全部弄清它之前.里面的各种配置,运行时总会出现想不到的意外. 总结一天学习的结果:程序正常运行.明白里面的原理.返回json和返回 ...
- django框架使用mysql报错,及两种解决方法
1.django框架 settings.py文件中部分代码: DATABASES = {# 'default': {# 'ENGINE': 'django.db.backends.sqlite3',# ...
- 已经安装了AccessDatabaseEngine.exe还是报错(两种原因)
1.安装(具体安装方式参考百度) 2.原因一:看版本 安装首先看VS中项目是多少版本(32还是64)对应安装 3.原因二:把目标平台(就是项目名字右键找属性)改成x64(主要看自己VS满足那个版本)
- Python 库安装问题-用pip安装pyHook3报错的两种常见问题和解决方法
pip install pyHook3 来安装 pyHook3,但是安装报错了. 问题一:缺少swig支持 我们来看报错信息,提示没有找到 swig.exe 这个文件. 原因是没有安装 swig.ex ...
- 415报错,两种解决方法
先确认后台逻辑有没有问题 没有的话考虑一下情况 第一种 查看消息头的响应头和请求头,看是否相同 如果不相同的话,继续修改,增加下面划重点部分 function getAddDirResponse(da ...
- IOS之UIToolBar约束报错
IOS之UIToolBar约束报错 . 使用纯代码创建UIToolBar,并已经设置toolBar1.translatesAutoresizingMaskIntoConstraints = false ...
- javaweb实训第五天下午——xml配置文件约束报错问题
spring框架中xml配置文件约束报错问题 问题描述: 问题描述: 如果电脑连不上网,或者网速不好可能会造成Spring框架中xml配置文件出现错误.但是这个错误不影响项目的运行的: <bea ...
- CMD终端关于pip报错,scrapy报错的一种处理方法
CMD终端关于pip报错,scrapy报错的一种处理方法 如果在终端输入pip,或scrapy,报如下错误: Fatal error in launcher: Unable to create pro ...
最新文章
- Android适应方案汇总(三)
- 【五线谱】踏板标记 ( 踩下踏板 Ped 标记 | 松开踏板 * 标记 | MIDI 中的对应踏板指令 | 连续控制信号 | 开关控制信号 )
- filter2D函数
- MSP430F5529 DriverLib 库函数学习笔记(四)UART通信
- JMeter实现web接口测试实践举例
- 网页遥控室内智能设备
- 贾老板大秀未来机器人之舞,场面有点不可描述(动图+视频)
- 时间序列分析python课程论文_python时间序列分析
- 修改默认启动操作系统
- 手机闪存速度排行_2020年手机性能排行榜前十
- “互联网+”医疗服务
- 入门力扣自学笔记180 C++ (题目编号:886)(涂色问题,可以多看看)
- 布同:如何循序渐进学习Python语言
- 一站式原创文章神器,让你轻松创作高质量文章
- PC版京东炸年兽活动一键做任务 全民自动炸年兽最新版1.1
- GitHub 标星超 26600,TiDB 社区运营的道与术!
- php后台登录,简单的PHP数据后台实现用户登录,php后台用户登录
- 简述async和await写法
- Vue 3的设计过程(翻译自尤雨溪原文)
- [转]ACM-ICPC比赛随想——刘汝佳