dzenbot / DZNEmptyDataSet

A drop-in UITableView/UICollectionView superclass category for showing empty datasets whenever the view has no content to display
https://www.cocoacontrols.com/controls/dznemptydataset
MIT License
12.09k stars 1.73k forks source link

Custom View not showing #141

Open the-civilian opened 9 years ago

the-civilian commented 9 years ago

I have a custom view defined via a xib file (defined with autolayout constraints), which I'm trying to use:

- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
  UIView *rootView = [[[NSBundle mainBundle] loadNibNamed:@"EmptyDataView"
                                                    owner:self
                                                  options:nil] objectAtIndex:0];

  return rootView;
}

For now the xib has only an UiView with an UILabel inside vertically and horizontally centered.

Am I doing something wrong?

mknet commented 9 years ago

I got a similar issue. The view just does not show up...

mknet commented 9 years ago

This pull request does fix my issue: https://github.com/dzenbot/DZNEmptyDataSet/pull/133

dzenbot commented 9 years ago

It does? Thanks for checking @mknet Will test the PR a bit more. Sorry about this issue.

mknet commented 9 years ago

Yeah, it does. I am not into iOS development as you are, so I can barely say what this line does but it helps:

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|" options:0 metrics:nil views:@{@"contentView": self.contentView}]];

I guess it resizes the "view port" of the view to the max even if there is no content. Am I right?

BillCarsonFr commented 9 years ago

I expected the customView to take all available space by default. But when loading the view from a Nib it is not the case. I had to make the following change to make it work:

if (_customView) {

    // -------- CODE CHANGE START -----
    [_customView setTranslatesAutoresizingMaskIntoConstraints:NO]; /* default is !CGRectIsEmpty(view.frame) */
    [self removeAllConstraints];

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:nil views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|" options:0 metrics:nil views:views]];
    // -------- CODE CHANGE END -----

    [views setObject:_customView forKey:@"customView"];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[customView]|" options:0 metrics:nil views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[customView]|" options:0 metrics:nil views:views]];

    // Skips from any further configuration
    return [super updateConstraints];;
}
dzenbot commented 9 years ago

This makes sense. I think that code was missed with one merge. Mind PR it?

BillCarsonFr commented 9 years ago

Hi @dzenbot ,

Done, i also added some crazy sample with a custom Nib, it's not a real life example though.

https://github.com/BillCarsonFr/DZNEmptyDataSet/commit/21112f533f75104cbd0e57eb5a1f31933e005346

BR.

cashmash commented 9 years ago

hi @dzenbot when will you merge it :)?

alexfu commented 8 years ago

Any status on this?