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

why the customView didn't show? #261

Open CYZZ opened 8 years ago

CYZZ commented 8 years ago
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView
{
    UIView *view = [UIView new];
    view.frame = CGRectMake(300, 100, 200, 200);
    view.backgroundColor = [UIColor greenColor];

    return view;
}

the view is can't see.how to set customView,from top replace center。

rozum-dev commented 8 years ago

Seems like the constraints for contentView inside UIScrollView+EmptyDataSet.h are incomplete. The code on 920 line sets centerX, centerY and Width but no height for contentView that actually contains custom view.

NSLayoutConstraint *centerXConstraint = [self equallyRelatedConstraintWithView:self.contentView attribute:NSLayoutAttributeCenterX];
NSLayoutConstraint *centerYConstraint = [self equallyRelatedConstraintWithView:self.contentView attribute:NSLayoutAttributeCenterY];

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

// When a custom offset is available, we adjust the vertical constraints' constants
if (self.verticalOffset != 0 && self.constraints.count > 0) {
    centerYConstraint.constant = self.verticalOffset;
}

This means that contentView height is 0.

I'd propose get rid of that centre constrains and change them to something like that all in one with vertical offset

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:nil views:@{@"contentView": self.contentView}]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat: @"V:|-%f-[contentView]|",self.verticalOffset] options:0 metrics:nil views:@{@"contentView": self.contentView}]];
JanGorman commented 8 years ago

Changing the constraints as suggested fixes this one case but breaks all other use cases of the library. Since the view that is shown participates in auto layout you can instead create a UIView subclass and have it return a custom intrinsicContentSize.

jayvenn commented 7 years ago

Does someone have a sample project on how to create a custom view? It will be greatly appreciated :)

rozum-dev commented 7 years ago

Project no, but here's example of implementation And how the view is initialised.

You should just get rid of extra constarints

open func customView(forEmptyDataSet scrollView: UIScrollView!) -> UIView? {
    let emptyView : EmptyStateBaseView = EmptyStateBaseView.view(delegate: nil, viewSize: self.emptyStateViewSize)
    emptyView.emptyImageView.image = UIImage.init(named:"favorites_emptystate")
    emptyView.emptyTitleLabel.text = LS("my_zone_empty_favorites_title")
    emptyView.emptySubtitleLabelText = LS("my_zone_empty_favorites_sub_title")
    emptyView.emptySubtitleLabel.text = LS("my_zone_empty_favorites_sub_title")
    emptyView.containerWidthLayoutConstaint.constant = self.traitCollection.horizontalSizeClass != .compact ? 380 : scrollView.size.width - 40.0
    return emptyView

}

On Jan 18, 2017, at 10:40, Jayven N notifications@github.com wrote:

Does someone have a sample project on how to create a custom view? It will be greatly appreciated :)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

jayvenn commented 7 years ago

I just want to return a UIView that fills my collection view with a background of black color

supermnemonic commented 7 years ago

i tried this hack and worked. i think there is something wrong with lib.

func customView(forEmptyDataSet scrollView: UIScrollView!) -> UIView! {
    let view = UIView()
    view.backgroundColor = UIColor.black
    view.frame = CGRect(x: 0, y: 0, width: scrollView.bounds.width, height: scrollView.bounds.height)
    scrollView.addSubview(view)
    return nil
}
JiamingTu commented 6 years ago

CustomVeiw must add height constriant,frame dosen't work