gpt4 book ai didi

ios - 更正 UITableViewHeaderFooterView 的子类化和自动布局

转载 作者:行者123 更新时间:2023-11-29 10:46:51 25 4
gpt4 key购买 nike

这是我在 UITableViewHeaderFooterView View 中设置内容的方法。

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self.contentView addSubview:self.createHeader];
}
return self;
}

- (UIView *)createHeader
{
UIView *headerContainer = [[UIView alloc] init];
headerContainer.backgroundColor = [UIColor blackColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.text = @"titlename";

[headerContainer addSubview:label];

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:headerContainer
attribute:NSLayoutAttributeLeft
multiplier:1.f constant:16.f];
[headerContainer addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:headerContainer
attribute:NSLayoutAttributeRight
multiplier:1.f constant:-16.f];
[headerContainer addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:headerContainer
attribute:NSLayoutAttributeTop
multiplier:1.f constant:0.f];
[headerContainer addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:headerContainer
attribute:NSLayoutAttributeBottom
multiplier:1.f constant:0.f];
[headerContainer addConstraint:constraint];

return headerContainer;
}

我收到此自动布局错误:

 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:0xd4bd7e0 UILabel:0x17254980.leading == UIView:0x172547d0.left + 16>",
"<NSLayoutConstraint:0xd4bd810 UILabel:0x17254980.trailing == UIView:0x172547d0.right - 16>",
"<NSAutoresizingMaskLayoutConstraint:0xd402510 h=--& v=--& H:[UIView:0x172547d0(0)]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xd4bd810 UILabel:0x17254980.trailing == UIView:0x172547d0.right - 16>

最佳答案

线索就在错误中:translatesAutoresizingMaskIntoConstraints

我会说您需要将以下行添加到您的 createHeader 方法中:

headerContainer.translatesAutoresizingMaskIntoConstraints = NO;

这是第二个想法。看起来您想将标签居中放在 headerContainer 中,对吗?而不是:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:headerContainer
attribute:NSLayoutAttributeLeft
multiplier:1.f constant:16.f];
[headerContainer addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeTrailing
relatedBy: NSLayoutRelationLessThanOrEqual
toItem:headerContainer
attribute:NSLayoutAttributeRight
multiplier:1.f constant:-16.f];
[headerContainer addConstraint:constraint];

尝试:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:headerContainer
attribute:NSLayoutAttributeLeft
multiplier:1.f constant:16.f];
[headerContainer addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:label
attribute:NSLayoutAttributeLeading
multiplier:1.f constant:0];
[headerContainer addConstraint:constraint];

关于ios - 更正 UITableViewHeaderFooterView 的子类化和自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22110491/

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