我正在向UICollectionViewCell添加子視圖。我正在編程添加約束來填充單元格,但寬度不填充單元格。子視圖寬度不適合superview與自動佈局
當我在視圖調試器中查看它時,它表示位置不明確。這是怎麼回事,因爲我指定所有4面都固定在超視圖上?
這就是調試器中的視圖。內,白色視圖應該適合父母寬度(藍色邊框):
檢查在父視圖的約束示出該與「位置模糊」警告:
我正在使用的代碼如下:
[self.contentView addSubview:calloutView];
calloutView.translatesAutoresizingMaskIntoConstraints = NO;
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[calloutView]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(calloutView) ]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[calloutView]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(calloutView) ]];
凡你使用這個代碼? –
UICollectionViewDelegate創建一個單元格,然後調用包含此代碼的單元格設置方法 – zorro2b
,但此代碼位於您的單元格內或您的viewController中? –