当我尝试使用这样的自动布局时:
CJGBoardLabels* labels = [CJGBoardLabels alloc];
[self.view insertSubview:labels aboveSubview:_mainBoard];
[_mainBoard addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[labels]|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(labels)]];
我收到此错误:
2014-01-02 16:51:05.141 chess[18050:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Requesting the window of a view (<CJGBoardLabels: 0xa6a8570; frame = (0 0; 0 0); transform = [0, 0, 0, 0, 0, 0]; alpha = 0; opaque = NO; layer = (null)>) with a nil layer. This view probably hasn't received initWithFrame: or initWithCoder:.'
在不调用initWithFrame的情况下以编程方式使用自动布局的适当方法是什么?或者我还需要调用initWithFrame吗?
答案 0 :(得分:1)
你做
labels = [CJBoardLabels new];
或
labels = [[CJBoardLabels alloc] init];
或
labels = [[CJBoardLabels alloc] initWithFrame:CGRectZero];
在每种情况下,接着是(重要)
labels.translatesAutoresizingMasksIntoConstraints = NO;
或者,您可以使用my lovely auto layout category并使用
labels = [CJBoardLabels autolayoutView];
为您做上述事情......以及在代码中创建约束的许多其他好东西。