Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Custom View centerX not working #427

Open
Shawkaine opened this issue Dec 20, 2018 · 1 comment
Open

Custom View centerX not working #427

Shawkaine opened this issue Dec 20, 2018 · 1 comment

Comments

@Shawkaine
Copy link

Shawkaine commented Dec 20, 2018

My customView is a view which contains a UIImageView and a label above both centering, and I return it at - (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView

However, the view's x is 0 instead of center, y works fine.

I tried to modify it in - (void)setupConstraints { ... if (_customView) {
using

 [self.contentView addConstraint:[self.contentView equallyRelatedConstraintWithView:_customView attribute:NSLayoutAttributeCenterX]];

the customView's right became clinging to the content view. Then I tried below

 if (_customView) {
        CGFloat padding = self.bounds.size.width/2-_customView.bounds.size.width/2;
        
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(padding@750)-[customView(>=10)]-(padding@750)-|" options:0 metrics:@{@"padding":@(padding)} views:@{@"customView":_customView}]];

This worked.

But it's really weird that I have to go through all this, did I do something wrong? Or is there a bug?

@KeymonWong
Copy link

My customView is a view which contains a UIImageView and a label above both centering, and I return it at - (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView

However, the view's x is 0 instead of center, y works fine.

I tried to modify it in - (void)setupConstraints { ... if (_customView) {
using

 [self.contentView addConstraint:[self.contentView equallyRelatedConstraintWithView:_customView attribute:NSLayoutAttributeCenterX]];

the customView's right became clinging to the content view. Then I tried below

 if (_customView) {
        CGFloat padding = self.bounds.size.width/2-_customView.bounds.size.width/2;
        
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(padding@750)-[customView(>=10)]-(padding@750)-|" options:0 metrics:@{@"padding":@(padding)} views:@{@"customView":_customView}]];

This worked.

But it's really weird that I have to go through all this, did I do something wrong? Or is there a bug?

- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView, when u use this method to create a custom view, u should remember to use Autolayout to layout your subviews, such as using Masonry, Because inner DZNEmptyDataSet use NSLayoutConstraint to layout view.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants