UITableViewHeader上的自动布局

Joh*_*Doe 7 objective-c uitableview ios autolayout

我已向Apple提交了有关此问题的错误报告!

我正在尝试在我的UITableViewHeader上使用新的iOS 6 Auto Layout,但是我抛出的所有东西都会在帖子结尾处返回错误.

我的代码:

TBMTableViewController.h

#import <UIKit/UIKit.h>

@interface TBMTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@end
Run Code Online (Sandbox Code Playgroud)

TBMTableViewController.m

#import "TBMTableViewController.h"
#import "UIView+Constraint.h"

@implementation TBMTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITableViewController *tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    [tableViewController.tableView setTranslatesAutoresizingMaskIntoConstraints:NO];

    UITableView *tableView = tableViewController.tableView;
    [tableView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [tableView setDelegate:self];
    [tableView setDataSource:self];

    [self.view addSubview:tableView];

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200.0f)];
    [headerView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [tableView setTableHeaderView:headerView];

    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn1 setTranslatesAutoresizingMaskIntoConstraints:NO];
    [btn1 setTitle:@"Button 1" forState:UIControlStateNormal];
    [btn1 sizeToFit];
    [headerView addSubview:btn1];

    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn2 setTranslatesAutoresizingMaskIntoConstraints:NO];
    [btn2 setTitle:@"Button 2" forState:UIControlStateNormal];
    [btn2 sizeToFit];
    [headerView addSubview:btn2];

    NSDictionary *views = NSDictionaryOfVariableBindings(tableView, headerView, btn1, btn2);

    [self.view addVisualConstraints:@"H:|[tableView]|" forViews:views];
    [self.view addVisualConstraints:@"V:|[tableView]|" forViews:views];

    [headerView addVisualConstraints:@"H:[btn1]-|" forViews:views];
    [headerView addVisualConstraints:@"V:|-[btn1]" forViews:views];
    [headerView addVisualConstraints:@"H:[btn2]-|" forViews:views];
    [headerView addVisualConstraints:@"V:[btn1]-[btn2]" forViews:views];
    [headerView addVisualConstraints:@"[btn2(==btn1)]" forViews:views];
}

@end
Run Code Online (Sandbox Code Playgroud)

UIView的+ Constraint.m

- (void)addVisualConstraints:(NSString *)constraintString forViews:(NSDictionary *)views {
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraintString
                                                                 options:0
                                                                 metrics:0
                                                                   views:views]];
}
Run Code Online (Sandbox Code Playgroud)

错误:

2013-02-01 00:43:34.481 ConstraintTest[10217:c07] *** Assertion failure in 
-[UITableView layoutSublayersOfLayer:], 
/SourceCache/UIKit_Sim/UIKit-2380.17/UIView.m:5776
2013-02-01 00:43:34.483 ConstraintTest[10217:c07] *** Terminating app due to 
uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still 
required after executing -layoutSubviews. UITableView's implementation of  
-layoutSubviews needs to call super.'

*** First throw call stack:
(0x1c94012 0x10d1e7e 0x1c93e78 0xb67665 0x6639f 0x10e56b0 0x2290fc0 0x228533c  
0x2290eaf 0x1052bd 0x4db56 0x4c66f 0x4c589 0x4b7e4 0x4b61e 0x4c3d9 0x4f2d2  
0xf999c 0x46574 0x4676f 0x46905 0x4f917 0x2b15 0x13157 0x13747 0x1494b 0x25cb5  
0x26beb 0x18698 0x1befdf9 0x1befad0 0x1c09bf5 0x1c09962 0x1c3abb6 0x1c39f44  
0x1c39e1b 0x1417a 0x15ffc 0x28dd 0x2805)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Run Code Online (Sandbox Code Playgroud)

我已经尝试了所有我能想到的东西.即使谷歌也没有帮助提供答案.我删除了非相关代码,如UITableViewDataSource-和UITableViewDelegate方法,以便澄清.没有涉及nibs,我的appDelegate唯一做的就是初始化TBMTableViewController并将其设置为rootViewController.

编辑: 我已经更新了我的viewDidLoad方法,以澄清我想对UITableViewHeader中的控件使用约束,而不是标题本身.同样的错误:)

Joh*_*Doe 8

这对我来说是一个非常愚蠢的错误.我删除线条时最终让一切正常:

[tableView setTranslatesAutoresizingMaskIntoConstraints:NO];
Run Code Online (Sandbox Code Playgroud)

[headerView setTranslatesAutoresizingMaskIntoConstraints:NO];
Run Code Online (Sandbox Code Playgroud)

  • 虽然这解决了你的问题,但它确实提出了一个关于这行代码的一个重要问题:你到底如何使用这段代码来阻止模糊的布局问题!?! (3认同)
  • [headerView layoutSubviews]; 当你调用addSubview时调用它 (2认同)