2014-10-30 14 views
6

Tôi đang cố gắng tìm cách sạch nhất để điều chỉnh chiều cao của tableHeaderView của UITableView, mà chính nó chỉ là một khung nhìn với nhãn nhiều dòng. Nó có vẻ như autolayout sẽ có thể tìm ra chiều cao khá dễ dàng.UIView-đóng gói-bố trí-chiều cao: ràng buộc không hợp lệ trong tableHeaderView

tôi tạo nhãn như vậy:

DDInsetLabelView *header = [[DDInsetLabelView alloc] initWithFrame:CGRectZero]; 
header.insets = UIEdgeInsetsMake(10.f, 10.f, 10.f, 10.f); 
header.label.text = [NSString stringWithFormat:@"%@\n%@", self.categoryTitle, self.subcategoryTitle]; 
header.label.numberOfLines = 0; 
header.label.textAlignment = NSTextAlignmentCenter; 
self.tableView.tableHeaderView = header; 

trong lớp nhãn, tôi xử lý bố trí như sau:

- (void)updateConstraints { 
    [super updateConstraints]; 

    NSDictionary *metrics = @{@"t": @(self.insets.top), @"l": @(self.insets.left), 
           @"b": @(self.insets.bottom), @"r": @(self.insets.right)}; 
    NSDictionary *views = NSDictionaryOfVariableBindings(_label); 
    self.label.translatesAutoresizingMaskIntoConstraints = NO; 
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(t)-[_label]-(b)-|" options:0 metrics:metrics views:views]]; 
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(l)-[_label]-(r)-|" options:0 metrics:metrics views:views]]; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    self.label.preferredMaxLayoutWidth = self.label.frame.size.width; 
    [super layoutSubviews]; 
} 

Bây giờ, đây là cách tôi đang tính toán chiều cao cho quan điểm tiêu đề trong bộ điều khiển của tôi:

- (void)viewDidLayoutSubviews { 
    [super viewDidLayoutSubviews]; 
    UIView *header = self.tableView.tableHeaderView; 
    NSLayoutConstraint *headerWidthConstraint = [NSLayoutConstraint 
               constraintWithItem:header attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual 
               toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:header.frame.size.width]; 
    [header addConstraint:headerWidthConstraint]; 
    CGFloat height = [header systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; 
    [header removeConstraint:headerWidthConstraint]; 

    header.frame = CGRectMake(0, 0, header.frame.size.width, height); 
    header.translatesAutoresizingMaskIntoConstraints = YES; 
    self.tableView.tableHeaderView = header; 
} 

Dường như tho này hoạt động tốt trực quan, nhưng tôi vẫn tiếp tục lỗi phát sinh trong bảng điều khiển:

Unable to simultaneously satisfy constraints. 
    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:0x7fb2e5bb7540 'UIView-Encapsulated-Layout-Height' V:[DDInsetLabelView:0x7fb2e5b53250(0)]>", 
    "<NSLayoutConstraint:0x7fb2e5bb84b0 V:|-(10)-[UILabel:0x7fb2e5b65200'Custom Box\nCustomize with...'] (Names: '|':DDInsetLabelView:0x7fb2e5b53250)>", 
    "<NSLayoutConstraint:0x7fb2e5bb8500 V:[UILabel:0x7fb2e5b65200'Custom Box\nCustomize with...']-(10)-| (Names: '|':DDInsetLabelView:0x7fb2e5b53250)>" 
) 

Tôi giả định rằng UIView-Encapsulated-Layout-Height đại diện cho giá trị độ cao được tính toán trong tiêu đề. Làm thế nào tôi có thể tránh cảnh báo này trong khi sử dụng autolayout để tính toán chiều cao của tiêu đề của tôi ??

+0

Có vấn đề này như nhau, nhưng với một cách tiếp cận đơn giản hơn. Sử dụng Xcode 6.3.2, tôi có thể chỉ gán bố cục tự động, bố cục xib của tôi cho thuộc tính 'tableHeaderView'. Chiều cao và chiều rộng chỉ hoạt động. Tuy nhiên, khi tôi segue để xem khác, sau đó trở lại, tôi nhận được rằng cùng một cảnh báo NSLayoutConstraint, và 'tableHeaderView' thay đổi chiều cao không có lý do ... đặc biệt là 0. :( –

Trả lời

0

Cố gắng loại bỏ các dòng ... header.translatesAutoresizingMaskIntoConstraints = YES;

Ngoài ra, thêm [header setTranslatesAutoresizingMaskIntoConstraints:NO] khi tạo tiêu đề của bạn

Các vấn đề liên quan