iOS AutoLayout报错包含UIView-Encapsulated-Layout-Width
2015-07-29 16:22
627 查看
原创Blog,转载请注明出处
blog.csdn.net/hello_hwc
这是今天做项目的时候遇到的一个问题,这里写下来,希望以后有人遇到了相似问题也能够快速解决。
先看看我的log
定位到相关代码
这里的`_collectionview`是一个UICollectionView
原则上这种代码不会出问题的啊。于是乎我stackoverflow一下。发现这个答案和我的很相似。原理是这样的
CollectionView会默认添加两个约束就是UIView-
例如我的代码改成这样子就没问题了
blog.csdn.net/hello_hwc
这是今天做项目的时候遇到的一个问题,这里写下来,希望以后有人遇到了相似问题也能够快速解决。
先看看我的log
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:0x7fe1ac9600a0 H:|-(103)-[UICollectionView:0x7fe1ab938c00] (Names: '|':HorizontalScrollTableviewHeader:0x7fe1aa68f120 )>", "<NSLayoutConstraint:0x7fe1ac95fe70 H:[UICollectionView:0x7fe1ab938c00]-(0)-| (Names: '|':HorizontalScrollTableviewHeader:0x7fe1aa68f120 )>", "<NSLayoutConstraint:0x7fe1ac966a00 H:|-(0)-[HorizontalScrollTableviewHeader:0x7fe1aa68f120] (Names: '|':TodayNewStampApplyHeader:0x7fe1ac87aab0 )>", "<NSLayoutConstraint:0x7fe1ac966a50 H:[HorizontalScrollTableviewHeader:0x7fe1aa68f120]-(0)-| (Names: '|':TodayNewStampApplyHeader:0x7fe1ac87aab0 )>", "<NSLayoutConstraint:0x7fe1aa691d00 'UIView-Encapsulated-Layout-Width' H:[TodayNewStampApplyHeader:0x7fe1ac87aab0(0)]>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7fe1ac95fe70 H:[UICollectionView:0x7fe1ab938c00]-(0)-| (Names: '|':HorizontalScrollTableviewHeader:0x7fe1aa68f120 )> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
定位到相关代码
NSArray * collectionConstraints_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_collectionview]-0-|" options:0 metrics:nil views:viewsDic]; [self addConstraints:collectionConstraints_V]; NSArray * collectionConstraints_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-103-[_collectionview]-0-|" options:0 metrics:nil views:viewsDic]; [self addConstraints:collectionConstraints_H];
这里的`_collectionview`是一个UICollectionView
原则上这种代码不会出问题的啊。于是乎我stackoverflow一下。发现这个答案和我的很相似。原理是这样的
CollectionView会默认添加两个约束就是UIView-
Encapsulated-Layout-Width和
UIView-Encapsulated-Layout-Hight保证大小适中。例如,我在我的约束里面添加了左右都对齐到Superview,可能这样做后宽度是210.1,而默认添加的约束会根据Itemsize取整为200。这样两个约束就发生冲突了。
解决方案
修改优先级,让自己的创建的冲突约束优先级低一些例如我的代码改成这样子就没问题了
NSArray * collectionConstraints_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_collectionview]-0-|" options:0 metrics:nil views:viewsDic]; [self addConstraints:collectionConstraints_V]; NSArray * collectionConstraints_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-103-[_collectionview]" options:0 metrics:nil views:viewsDic]; [self addConstraints:collectionConstraints_H]; NSLayoutConstraint * constraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[_collectionview]-0-|" options:0 metrics:nil views:viewsDic].firstObject; constraint.priority = UILayoutPriorityDefaultLow; [self addConstraint:constraint];
相关文章推荐
- 黑马程序员——高新技术---Java基础-GUI编程
- UIKit框架-12.UITableView数据刷新
- Ubiquitous Religions(POJ--2524
- Unique Path II
- IOS开发概述-09.UIController和控制器的View
- 【进阶android】Volley源码分析——Volley的工具【StringRequest】
- iOS开发使用Unwind Segue进行返回
- Spark SQL and DataFrame Guide(1.4.1)——之DataFrames
- UIActionSheet
- 云盘+Git GUI云盘文件版本控制
- ios开发之实现长按UITableViewCell弹出UIMenuController
- Android Build属性介绍
- apue 第十一章 线程
- C#中Queue<T>类的使用以及部分方法的源码分析
- C#中Queue<T>类的使用以及部分方法的源码分析
- muduo::BlockingQueue、BoundedBlockingQueue分析
- SPOJ 1771 NQUEEN Yet Another N-Queen Problem
- 通用Key-Value存储系统的存储管理策略解析
- Architecture,Valid architectures,Build Active Architecture Only
- 配置文件读取