当我添加自动布局时,视图消失了
When I add autolayout, the views disapear
我总共有 1 个 label
和 1 个 UICollectionView
view
。我正在尝试让它们像下图一样:
我无法使用情节提要来应用自动布局,我必须以编程方式进行,但我很难做到。这是我拥有的:
CGRect collectionViewFrame = CGRectMake(0, 20, self.bounds.size.width, self.bounds.size.height - 20);
_datesCollectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:collectionViewLayout];
_labelToChange = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 20)];
[_labelToChange setTranslatesAutoresizingMaskIntoConstraints:NO];
[datesCollectionView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_labelToChange]-[_datesCollectionView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelToChange, _datesCollectionView)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_labelToChange]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelToChange)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_labelToChange]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelToChange)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_datesCollectionView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_datesCollectionView)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_datesCollectionView]|"options:0 metrics:nil views:NSDictionaryOfVariableBindings(_datesCollectionView)]];
当我在模拟器上 运行 时,我没有看到任何视图,并且出现以下错误:
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:0x7fa080d801b0 H:|-(0)-[DIDatepicker:0x7fa080d7f9a0] (Names: '|':UITableViewCellContentView:0x7fa080d7f360 )>",
"<NSLayoutConstraint:0x7fa080d80250 H:[DIDatepicker:0x7fa080d7f9a0]-(0)-| (Names: '|':UITableViewCellContentView:0x7fa080d7f360 )>",
"<NSLayoutConstraint:0x7fa080da9120 H:[UILabel:0x7fa080da8350]-(0)-[UICollectionView:0x7fa081159000]>",
"<NSLayoutConstraint:0x7fa080daa7e0 H:[UILabel:0x7fa080da8350]-(0)-| (Names: '|':DIDatepicker:0x7fa080d7f9a0 )>",
"<NSLayoutConstraint:0x7fa080daab50 H:|-(0)-[UICollectionView:0x7fa081159000] (Names: '|':DIDatepicker:0x7fa080d7f9a0 )>",
"<NSLayoutConstraint:0x7fa080db70c0 'fittingSizeHTarget' H:[UITableViewCellContentView:0x7fa080d7f360(375)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fa080da9120 H:[UILabel:0x7fa080da8350]-(0)-[UICollectionView:0x7fa081159000]>
我做错了什么,我该如何解决?
这一行有歧义:
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_labelToChange]-[_datesCollectionView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelToChange, _datesCollectionView)]];
并且您没有为视图定义任何宽度或高度信息。 AutoLayout 没有足够的信息来布置您的视图。
试试这个:
NSDictionary *dict = NSDictionaryOfVariableBindings(_labelToChange, _datesCollectionView);
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_labelToChange(80)]-0-[_datesCollectionView]-0-|" options:0 metrics:nil views:dict]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_labelToChange]-0-|" options:0 metrics:nil views:dict]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_datesCollectionView]-0-|" options:0 metrics:nil views:dict]];
我在这里所做的是,我将标签定义为 80 像素高,然后 collectionview 占据了 superview 的其余高度。
你的约束:
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_labelToChange]-[_datesCollectionView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelToChange, _datesCollectionView)]];
与约束冲突:
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_labelToChange]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelToChange)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_datesCollectionView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_datesCollectionView)]];
您需要如下所示替换约束:
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_labelToChange(20)]-0-[_datesCollectionView]-0-|" options:0 metrics:nil views:dict]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_labelToChange]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelToChange)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_datesCollectionView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_datesCollectionView)]];
希望对您有所帮助..:)
我总共有 1 个 label
和 1 个 UICollectionView
view
。我正在尝试让它们像下图一样:
我无法使用情节提要来应用自动布局,我必须以编程方式进行,但我很难做到。这是我拥有的:
CGRect collectionViewFrame = CGRectMake(0, 20, self.bounds.size.width, self.bounds.size.height - 20);
_datesCollectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:collectionViewLayout];
_labelToChange = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 20)];
[_labelToChange setTranslatesAutoresizingMaskIntoConstraints:NO];
[datesCollectionView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_labelToChange]-[_datesCollectionView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelToChange, _datesCollectionView)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_labelToChange]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelToChange)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_labelToChange]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelToChange)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_datesCollectionView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_datesCollectionView)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_datesCollectionView]|"options:0 metrics:nil views:NSDictionaryOfVariableBindings(_datesCollectionView)]];
当我在模拟器上 运行 时,我没有看到任何视图,并且出现以下错误:
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:0x7fa080d801b0 H:|-(0)-[DIDatepicker:0x7fa080d7f9a0] (Names: '|':UITableViewCellContentView:0x7fa080d7f360 )>",
"<NSLayoutConstraint:0x7fa080d80250 H:[DIDatepicker:0x7fa080d7f9a0]-(0)-| (Names: '|':UITableViewCellContentView:0x7fa080d7f360 )>",
"<NSLayoutConstraint:0x7fa080da9120 H:[UILabel:0x7fa080da8350]-(0)-[UICollectionView:0x7fa081159000]>",
"<NSLayoutConstraint:0x7fa080daa7e0 H:[UILabel:0x7fa080da8350]-(0)-| (Names: '|':DIDatepicker:0x7fa080d7f9a0 )>",
"<NSLayoutConstraint:0x7fa080daab50 H:|-(0)-[UICollectionView:0x7fa081159000] (Names: '|':DIDatepicker:0x7fa080d7f9a0 )>",
"<NSLayoutConstraint:0x7fa080db70c0 'fittingSizeHTarget' H:[UITableViewCellContentView:0x7fa080d7f360(375)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fa080da9120 H:[UILabel:0x7fa080da8350]-(0)-[UICollectionView:0x7fa081159000]>
我做错了什么,我该如何解决?
这一行有歧义:
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_labelToChange]-[_datesCollectionView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelToChange, _datesCollectionView)]];
并且您没有为视图定义任何宽度或高度信息。 AutoLayout 没有足够的信息来布置您的视图。
试试这个:
NSDictionary *dict = NSDictionaryOfVariableBindings(_labelToChange, _datesCollectionView);
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_labelToChange(80)]-0-[_datesCollectionView]-0-|" options:0 metrics:nil views:dict]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_labelToChange]-0-|" options:0 metrics:nil views:dict]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_datesCollectionView]-0-|" options:0 metrics:nil views:dict]];
我在这里所做的是,我将标签定义为 80 像素高,然后 collectionview 占据了 superview 的其余高度。
你的约束:
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_labelToChange]-[_datesCollectionView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelToChange, _datesCollectionView)]];
与约束冲突:
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_labelToChange]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelToChange)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_datesCollectionView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_datesCollectionView)]];
您需要如下所示替换约束:
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_labelToChange(20)]-0-[_datesCollectionView]-0-|" options:0 metrics:nil views:dict]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_labelToChange]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_labelToChange)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_datesCollectionView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_datesCollectionView)]];
希望对您有所帮助..:)