gpt4 book ai didi

ios - 当我添加自动布局时, View 消失了

转载 作者:行者123 更新时间:2023-11-29 12:21:02 25 4
gpt4 key购买 nike

我在整体 View 中有 1 个 label 和 1 个 UICollectionView。我试图让他们像下图一样:

我无法使用 Storyboard 来应用自动布局,我必须以编程方式进行,但我很难做到。这是我所拥有的:

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)]];

当我在模拟器上运行它时,我没有看到任何 View ,并且出现以下错误:

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)]];

与约束冲突:

[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)]];

希望对您有所帮助..:)

关于ios - 当我添加自动布局时, View 消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30634700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com