Problem autorun view

I am loading a custom XIV UIView file as a uitableview header inside a view controller.

The file owner for the xib file is the view manager. I have both a viewcontroller and a uiview interface declared inside a 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. "; } 

The AutolayoutLabel class overrides the following method:

  - (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]; } } 

The height calculated by the system LayoutSizeFittingSize: UILayoutFittingCompressedSize returns 0. As a result, as a view of the table header, I get the following view: Header view with height 0

When I added the actual height as shown below, the uilabel overflows. I expect uiview to grow as the label height grows.

  headerFrame.size.height = self.sizingView.frame.size.height; 

TableHeaderView not being resized properly

Here is a screen shot of the limitations of UIViews:

ZeroState view XIB file with constraints

What am I missing here? Can someone point me?

Update I created a sample project so that you guys can check what exactly produces.

+5
source share
5 answers

I overestimated what you had so far differently. To get started, I removed the UIView in ZeroStateView.xib , in which UIImageView and UILabel were embedded. The xib base is already a UIView , so there is no need to add another UIView to it,

Then I changed the restrictions. I don’t remember exactly what restrictions I changed, so I just listed them here:

  • Limitations for UIImageView
    • Align Center X to Superview
    • Width = 60
    • Height = 56
    • Upper Observation Space = 37
    • Lower space to UILabel = 31
  • Limitations for UILabel
    • Left observation space = 15
    • Right observation space = 15
    • Lower observation space = 45
    • Top space up to UIImageView = 31

To the code. In ViewController.h IBOutlet did nothing as far as I could tell, so I changed this property to read @property (strong, nonatomic) ZeroStateView *zeroStateView;

Now important changes: ViewController.m . There are two UITableViewDelegate methods that will replace prepareHeaderViewForZeroState . In viewDidLoad initialize zeroStateView and set the table view delegate to self .

 - (void)viewDidLoad { //... // Load the view self.zeroStateView = [[[NSBundle mainBundle] loadNibNamed:@"ZeroStateView" owner:self options:nil] firstObject]; [self.zeroStateView updateView]; self.zeroStateView.backgroundColor = [UIColor darkGrayColor]; // Set self for table view delegate for -heightForHeaderInSection: and viewForHeaderInSection: self.dataTable.delegate = self; } 

Now that we are a delegate to the table, we get two method calls that will allow us to customize the presentation of the header and set its height accordingly.

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { // This will set the header view to the zero state view we made in viewDidLoad return self.zeroStateView; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { // sizeToFit describes the size that the label can fit itself into. // So we are saying that the label can use the width of the view and infinite height. CGSize sizeToFit = CGSizeMake(self.view.frame.size.width, MAXFLOAT); // Then we ask the label to tell us how it can fit in that size. // The label will respond with the width of the view and however much height it needs // to display its text. This is the magic of how it grows vertically. CGSize size = [self.zeroStateView.titleLabel sizeThatFits:sizeToFit]; // Add the height the label needs to the overall zero state view. This should be changed // to the height of the UIImage + the height we just got + the whitespace above and below // each of these views. You can handle that part. return self.zeroStateView.frame.size.height + size.height; } 

I uploaded my changes to Dropbox here .

+2
source

By simply resizing the frame of the HeaderView table, it will not resize it. You must install it again in the View table to force a reboot.

You must call this again self.tableView.tableHeaderView = headerView; after setting a new frame size.

+1
source

As you call the prepareHeaderViewForZeroState method from viewwillappear . At this point, the layout is not calculated. so there is a force layout to calculate before calling the systemLayoutSizeFittingSize method to calculate the cell height. Here is the code you need to write before calling systemLayoutSizeFittingSize .

 UIView *header = self.tableView.tableHeaderView; [header setNeedsLayout]; [header layoutIfNeeded]; 

Edit:

You have left 1 restriction in ZeroStateView.xib . i.e. Bottom Space to : Superview . kindly see the screenshot.

enter image description here

Exit:

enter image description here

Here you updated code

Hope this helps you.

+1
source

I'm not sure, but you can check if you have upper, final, upper and lower limits for UIImage and labels with a link to the supervisor.

Edit: Add a width limit before getting systemLayoutSize

 NSLayoutConstraint *tempWidthConstraint = [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:CGRectGetWidth(window.frame)]; widthConstraint.constant = tempWidthConstraint.constant; [self.contentView addConstraint:tempWidthConstraint]; CGSize fittingSize = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; CGFloat height = fittingSize.height +1; [self.contentView removeConstraint:tempWidthConstraint]; 
0
source

There are several things that bother me, which can complicate the situation.

  • Why do you subclass a label?
  • Limitations do not really make sense.
    • Why does the label have two height restrictions, constant and β‰₯ restriction? Remove them both, you also do not need.
    • In addition, your vertical space at the bottom of the container is also β‰₯. What for? Be that as it may, your custom view does not have a certain height, which may be due to the fact that the size of the fitting returns the height of zero.

As soon as you solve these problems, let me know if you are more fortunate.

0
source

All Articles