Tôi đang tải một tập tin tùy chỉnh XIB UIView như một cái nhìn tiêu đề của một UITableView bên trong một bộ điều khiển xem.Tableheader vấn đề xem autolayout
Chủ sở hữu tệp cho file xib là viewController. Tôi có cả & giao diện của viewController của UIView tuyên bố bên trong UIViewController.
ViewController.h
@class ZeroStateView;
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, weak) IBOutlet CustomUITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (weak, nonatomic) IBOutlet ZeroStateView *zeroStateView;
@end
@interface ZeroStateView : UIView
@property (weak, nonatomic) IBOutlet AutoLayoutLabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *titleIcon;
- (void)updateView;
@end
ViewController.m
- (void)prepareHeaderViewForZeroState{
ZeroStateView *sizingView = [ZeroStateView new];
[[[NSBundle mainBundle] loadNibNamed:@"ZeroStateView" owner:self options:nil] objectAtIndex:0];
sizingView = self.zeroStateView;
[sizingView updateView];
self.tableView.tableHeaderView = sizingView;
UIView *headerView = self.tableView.tableHeaderView;
CGFloat height = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGRect headerFrame = headerView.frame;
headerFrame.size.height = height;
headerView.frame = headerFrame;
self.tableView.tableHeaderView = headerView;
}
@end
@implementation ZeroStateView
-(void)updateView{
self.titleIcon.alpha = 0.5;
UIFontDescriptor *titleFontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleSubheadline];
self.titleLabel.text = @"This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. ";
}
Lớp AutolayoutLabel các phương pháp sau đây ghi đè:
- (void)setBounds:(CGRect)bounds {
[super setBounds:bounds];
// For multiline label, preferredMaxLayoutWidth always matches the frame width
if (self.numberOfLines == 0 && bounds.size.width != self.preferredMaxLayoutWidth) {
self.preferredMaxLayoutWidth = self.bounds.size.width;
[self setNeedsUpdateConstraints];
}
}
Chiều cao tính theo systemLayoutSizeFittingSize: UILayoutFittingCompressedSize lợi nhuận 0. Như một kết quả tôi nhận được quan điểm sau khi xem bảng tiêu đề:
Khi tôi thêm chiều cao thực tế như dưới đây, các tràn UILabel. Tôi đang mong uiview phát triển khi chiều cao nhãn tăng lên.
headerFrame.size.height = self.sizingView.frame.size.height;
Dưới đây là ảnh chụp màn hình về điều đó hạn chế UIViews:
làm tôi nhớ gì ở đây? Ai đó có thể chỉ cho tôi không?
Cập nhật Tôi đã tạo mẫu project để các bạn kiểm tra chính xác vấn đề là gì.
Đối với những gì đáng giá, nó luôn giúp nêu rõ câu hỏi của bạn trong câu đầu tiên. –
Bạn không nên gọi đây là 'ZeroStateView * sizingView = [ZeroStateView new];' nếu bạn gán thứ gì đó khác cho nó 2 dòng sau. – pteofil
Khi nào bạn gọi 'prepareHeaderViewForZeroState'? – pteofil