DZNEmptyDataSet icon indicating copy to clipboard operation
DZNEmptyDataSet copied to clipboard

why the customView didn't show?

Open CYZZ opened this issue 9 years ago • 7 comments

- (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。

CYZZ avatar May 21 '16 03:05 CYZZ

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

rozum-dev avatar May 26 '16 23:05 rozum-dev

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.

JanGorman avatar Aug 25 '16 07:08 JanGorman

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

jayvenn avatar Jan 18 '17 08:01 jayvenn

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 [email protected] 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.

rozum-dev avatar Jan 18 '17 08:01 rozum-dev

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

jayvenn avatar Jan 18 '17 09:01 jayvenn

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
}

supermnemonic avatar Aug 29 '17 05:08 supermnemonic

CustomVeiw must add height constriant,frame dosen't work

JiamingTu avatar Dec 27 '17 10:12 JiamingTu