Sto caricando un file XIB di UIView personalizzato come vista di intestazione di una vista uitable in un controller di visualizzazione.Problema di caricamento automatico della vista Tableheader
Il proprietario del file per il file xib è il viewcontroller. Dispongo sia di & del viewcontroller che l'interfaccia di uiview è stata dichiarata all'interno di 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. ";
}
La classe AutolayoutLabel il seguente metodo sovrascritto:
- (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];
}
}
L'altezza calcolata dal systemLayoutSizeFittingSize: UILayoutFittingCompressedSize restituisce 0. Di conseguenza ottengo il seguente vista come la vista intestazione della tabella:
Quando ho aggiunto l'altezza reale come sotto, i overflow UILabel. Mi aspetto che la visualizzazione dei dati cresca man mano che l'altezza dell'etichetta aumenta.
headerFrame.size.height = self.sizingView.frame.size.height;
Ecco la cattura dello schermo di che i vincoli di UIViews:
Che cosa mi manca qui? Qualcuno può segnalarmi?
Aggiornamento ho creato un campione project per voi ragazzi per controllare che cosa è esattamente questione è.
Per quello che vale, aiuta sempre a indicare la tua domanda nella prima frase. –
Non si dovrebbe chiamare questo 'ZeroStateView * sizingView = [ZeroStateView new];' se si assegna qualcos'altro ad esso 2 righe dopo. – pteofil
Quando si chiama 'prepareHeaderViewForZeroState'? – pteofil