正确的子类和UITableViewHeaderFooterView的自动布局 [英] Correct subclassing and autolayout of UITableViewHeaderFooterView

查看:252
本文介绍了正确的子类和UITableViewHeaderFooterView的自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我如何在我UITableViewHeaderFooterView视图设置东西了。

   - (ID)initWithFrame:方法(的CGRect)帧
{
自= [超级initWithFrame:方法框架]
如果(个体经营){
    [self.contentView addSubview:self.createHeader];
}
返回自我;
} - (UIView的*)createHeader
{
  UIView的* headerContainer = [[UIView的页头]初始化];
  headerContainer.backgroundColor =的UIColor blackColor]
  *的UILabel标签= [[的UILabel页头] initWithFrame:方法CGRectZero];
  label.translatesAutoresizingMaskIntoConstraints = NO;
  label.text = @TITLENAME  [headerContainer addSubview:标号];  NSLayoutConstraint *约束= [NSLayoutConstraint constraintWithItem:标签
                                                                   属性:NSLayoutAttributeLeading
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:headerContainer
                                                              属性:NSLayoutAttributeLeft
                                                             事半功倍:1.F常数:16.f];
[headerContainer addConstraint:约束];约束= [NSLayoutConstraint constraintWithItem:标签
                                          属性:NSLayoutAttributeTrailing
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:headerContainer
                                          属性:NSLayoutAttributeRight
                                         事半功倍:1.F常数:-16.f];
[headerContainer addConstraint:约束];约束= [NSLayoutConstraint constraintWithItem:标签
                                          属性:NSLayoutAttributeTop
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:headerContainer
                                          属性:NSLayoutAttributeTop
                                         事半功倍:1.F常数:0.f];
[headerContainer addConstraint:约束];约束= [NSLayoutConstraint constraintWithItem:标签
                                          属性:NSLayoutAttributeBottom
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:headerContainer
                                          属性:NSLayoutAttributeBottom
                                         事半功倍:1.F常数:0.f];
[headerContainer addConstraint:约束];返回headerContainer;
}

我得到这个自动布局错误:

 大概在以下列表中的约束条件中的至少一个是一个你不想要的。
 试试这个:(1)看一下每个约束和揣摩,你不要指望;
 (2)找到code,它增加了不必要的约束或限制并修复它。
 (注:如果你看到你不明白NSAutoresizingMaskLayoutConstraints,
 参考文档UIView的财产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)]>中
)将试图打破约束恢复
< NSLayoutConstraint:0xd4bd810的UILabel:0x17254980.trailing ==的UIView:0x172547d0.right - 16取代;


解决方案

线索是正确的,在错误: translatesAutoresizingMaskIntoConstraints

我会说,你需要下面的行添加到您的 createHeader 方法:

headerContainer.translatesAutoresizingMaskIntoConstraints = NO;

下面是第二个想法。它看起来像要居中内headerContainer您的标签,对不对?相反的:

  NSLayoutConstraint *约束= [NSLayoutConstraint constraintWithItem:标签
                                                                   属性:NSLayoutAttributeLeading
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:headerContainer
                                                              属性:NSLayoutAttributeLeft
                                                             事半功倍:1.F常数:16.f];
[headerContainer addConstraint:约束];约束= [NSLayoutConstraint constraintWithItem:标签
                                          属性:NSLayoutAttributeTrailing
                                          relatedBy:NSLayoutRelationLessThanOrEqual
                                             toItem:headerContainer
                                          属性:NSLayoutAttributeRight
                                         事半功倍:1.F常数:-16.f];
[headerContainer addConstraint:约束];

尝试:

  NSLayoutConstraint *约束= [NSLayoutConstraint constraintWithItem:标签
                                                                   属性:NSLayoutAttributeLeading
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:headerContainer
                                                              属性:NSLayoutAttributeLeft
                                                             事半功倍:1.F常数:16.f];
[headerContainer addConstraint:约束];约束= [NSLayoutConstraint constraintWithItem:标签
                                          属性:NSLayoutAttributeTrailing
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:标签
                                          属性:NSLayoutAttributeLeading
                                         事半功倍:1.F常数:0];
[headerContainer addConstraint:约束];

Here's how I set stuff up in my 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;
}

I get this autolayout error:

 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>

解决方案

The clue is right there in the error: translatesAutoresizingMaskIntoConstraints.

I would say you need to add the following line to your createHeader method:

headerContainer.translatesAutoresizingMaskIntoConstraints = NO;

Here's a second idea. It looks like you want to center your label inside headerContainer, right? Instead of:

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

Try:

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

这篇关于正确的子类和UITableViewHeaderFooterView的自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆