Tôi đã đập đầu vào tường với thứ này trong 3 hoặc 4 giờ qua và dường như tôi không thể tìm ra. Tôi có UIViewController với UITableView toàn màn hình bên trong nó (có một số thứ khác trên màn hình, đó là lý do tại sao tôi không thể sử dụng UITableViewController) và tôi muốn tableHeaderView của mình thay đổi kích thước bằng tính năng tự động thanh toán. Không cần phải nói, nó không hợp tác.
Xem ảnh chụp màn hình bên dưới.
Bởi vì nhãn OverviewLabel (ví dụ: văn bản "Liệt kê thông tin tổng quan ở đây.") Có nội dung động, tôi đang sử dụng autolayout để thay đổi kích thước và nó là superview. Tôi đã có mọi thứ thay đổi kích thước một cách tuyệt vời, ngoại trừ tableHeaderView, nằm ngay bên dưới Paralax Table View trong quá trình tìm kiếm.
Cách duy nhất tôi đã tìm thấy để thay đổi kích thước chế độ xem tiêu đề đó là theo chương trình, với đoạn mã sau:
CGRect headerFrame = self.headerView.frame;
headerFrame.size.height = headerFrameHeight;
self.headerView.frame = headerFrame;
[self.listTableView setTableHeaderView:self.headerView];
Trong trường hợp này, headerFrameHeight là phép tính thủ công chiều cao tableViewHeader như sau (innerHeaderView là vùng trắng hoặc "Chế độ xem" thứ hai, headerView là tableHeaderView) :
CGFloat startingY = self.innerHeaderView.frame.origin.y + self.overviewLabel.frame.origin.y;
CGRect overviewSize = [self.overviewLabel.text
boundingRectWithSize:CGSizeMake(290.f, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName: self.overviewLabel.font}
context:nil];
CGFloat overviewHeight = overviewSize.size.height;
CGFloat overviewPadding = ([self.overviewLabel.text length] > 0) ? 10 : 0; // If there's no overviewText, eliminate the padding in the overall height.
CGFloat headerFrameHeight = ceilf(startingY + overviewHeight + overviewPadding + 21.f + 10.f);
Tính toán thủ công hoạt động, nhưng nó phức tạp và dễ bị sai nếu mọi thứ thay đổi trong tương lai. Những gì tôi muốn có thể làm là để tableHeaderView tự động thay đổi kích thước dựa trên các ràng buộc được cung cấp, giống như bạn có thể làm ở bất kỳ nơi nào khác. Nhưng đối với cuộc sống của tôi, tôi không thể hình dung được.
Có một số bài đăng trên SO về điều này, nhưng không có bài nào rõ ràng và cuối cùng khiến tôi khó hiểu hơn. Đây là một số:
Có thể sử dụng AutoLayout với tableHeaderView của UITableView không?
Chiều cao của chế độ xem tiêu đề bảng bị sai khi sử dụng bố cục tự động, IB và kích thước phông chữ
Không thực sự có ý nghĩa khi thay đổi thuộc tính translateAutoresizingMaskIntoConstraints thành KHÔNG, vì điều đó chỉ gây ra lỗi cho tôi và không có ý nghĩa gì về mặt khái niệm.
Bất kỳ trợ giúp sẽ thực sự được đánh giá cao!
CHỈNH SỬA 1: Nhờ gợi ý của TomSwift, tôi đã có thể tìm ra. Thay vì tính toán chiều cao của tổng quan theo cách thủ công, tôi có thể tính toán nó cho tôi như sau và sau đó thiết lập lại tableHeaderView như trước.
[self.headerView setNeedsLayout];
[self.headerView layoutIfNeeded];
CGFloat height = [self.innerHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + self.innerHeaderView.frame.origin.y; // adding the origin because innerHeaderView starts partway down headerView.
CGRect headerFrame = self.headerView.frame;
headerFrame.size.height = height;
self.headerView.frame = headerFrame;
[self.listTableView setTableHeaderView:self.headerView];
Chỉnh sửa 2: Như những người khác đã lưu ý, giải pháp được đăng trong Chỉnh sửa 1 dường như không hoạt động trong viewDidLoad. Tuy nhiên, nó dường như hoạt động trong viewWillLayoutSubviews. Mã ví dụ bên dưới:
// Note 1: The variable names below don't match the variables above - this is intended to be a simplified "final" answer.
// Note 2: _headerView was previously assigned to tableViewHeader (in loadView in my case since I now do everything programatically).
// Note 3: autoLayout code can be setup programatically in updateViewConstraints.
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
[_headerWrapper setNeedsLayout];
[_headerWrapper layoutIfNeeded];
CGFloat height = [_headerWrapper systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGRect headerFrame = _headerWrapper.frame;
headerFrame.size.height = height;
_headerWrapper.frame = headerFrame;
_tableView.tableHeaderView = _headerWrapper;
}
setTableHeaderView
không hoạt động trên Xcode6. Vấn đề là các ô bị chồng chéo bởi tableHeaderView. Tuy nhiên, nó hoạt động trên Xcode5