1
我正尝试以编程方式创建UICollectionViewCell
子类。我正在尝试创建自动布局支持。UICollectionViewCell以编程方式自动布局
我想要一个UIView
添加领先,顶部,尾部和底部约束的单元格的内容视图。但是当我这样做时,我的UIView's
宽度和高度保持为零。这里是我的内initWithFrame
方法代码:
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
_someView = [[UIView alloc] initWithFrame:CGRectZero];
_someView.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_someView];
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-2-[someView]-2-|" options:0 metrics:nil views:@{@"someView" : _someView}];
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-2-[someView]-2-|" options:0 metrics:nil views:@{@"someView" : _someView}];
[self.contentView addConstraints:horizontalConstraints];
[self.contentView addConstraints:verticalConstraints];
为什么这些限制是灰色的?
谢谢!
所有您的约束安装在您正在使用的任何大小类中?不确定可视调试器,但是IB/Storyboard有一个灰色的约束是项目中存在的约束,但没有以编辑器显示的任何大小类组合安装(默认为任何/任何) –
控制器是否抛出任何布局警告?同时在contentView上将'translatesAutoresizingMaskintoConstraints'设置为NO可能会破坏单元格布局系统。尽量不要这样做。 –
@WarrenBurton它的工作!谢谢!!!!请回答我的答案,我会投票并批准它! –