在UITableViewHeader自动布局 [英] Auto layout on UITableViewHeader

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

问题描述

我已经提交bug报告与苹果有关这个问题!

我试图使用新的iOS 6的自动布局在我UITableViewHeader,但一切我在它扔​​回来的错误在帖子的结尾。

I'm trying to use the new iOS 6 Auto Layout on my UITableViewHeader, but everything I throw at it comes back with the error at the end of the post.

我的code:

TBMTableViewController.h

#import <UIKit/UIKit.h>

@interface TBMTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@end

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

的UIView + Constraint.m

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

错误:

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)

我用尽了一切我能想到的。即使谷歌没有提供的答案有帮助。我已经删除了不相关的code,如UITableViewDataSource-和的UITableViewDelegate方法澄清。有没有涉及到笔尖和我的appDelegate做的唯一事情就是初始化的TBMTableViewController并将其设置为RootViewController的。

I've tried everything I can think of. Even Google wasn't helpful in providing an answer. I've removed the non-pertinent code such as UITableViewDataSource- and UITableViewDelegate methods for clarification. There are no nibs involved and the only thing my appDelegate does is init the TBMTableViewController and setting it as the rootViewController.

编辑:
我已经更新了我viewDidLoad方法澄清,我想在控件使用UITableViewHeader,而不是头本身的内部约束。同样的错误:)

I've updated my viewDidLoad method to clarify that I want to use constraints on controls inside the UITableViewHeader, not the header itself. Same error :)

推荐答案

这竟然是我的一个非常愚蠢的错误。我终于当我删除了一切行工作:

This turned out to be a really stupid error on my part. I eventually got everything working when I removed the lines:

[tableView setTranslatesAutoresizingMaskIntoConstraints:NO];

[headerView setTranslatesAutoresizingMaskIntoConstraints:NO];

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

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