DZNEmptyDataSet icon indicating copy to clipboard operation
DZNEmptyDataSet copied to clipboard

How to set the button's frame

Open ZhongYanga opened this issue 8 years ago • 3 comments

I want to set button's width by myself, how can i set it? Thank you for help me

ZhongYanga avatar Oct 12 '17 11:10 ZhongYanga

+1

czwpro avatar Oct 19 '17 09:10 czwpro

@ZhongYanga you can try set a button backgroundImage and set backgroundImage contentMode to UIViewContentModeScaleAspectFit

@implementation UIScrollView (DZNEmptyViewSize)

- (void)aspectFitEmtptyButtonSize {
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass: [NSClassFromString(@"DZNEmptyDataSetView") class]]) {
            UIView *emptyContentView = [[view subviews] firstObject];
            for (UIView *subView in emptyContentView.subviews) {
                if ([subView isKindOfClass:[UIButton class]]) {
                    for (UIView *childView in subView.subviews) {
                        if ([childView isKindOfClass:[UIImageView class]]) {
                            childView.contentMode = UIViewContentModeScaleAspectFit;
                        }
                    }
                }
            }
        }
    }
}

@end
#pragma mark - DZNEmptyDataSetDelegate
- (void)emptyDataSetDidAppear:(UIScrollView *)scrollView {
    [scrollView aspectFitEmtptyButtonSize];
}

czwpro avatar Oct 20 '17 07:10 czwpro

implementation emptyDataSetDidAppear: method, don't forget setting emptyDataSetDelegate.

- (void)emptyDataSetDidAppear:(UIScrollView *)scrollView {
    UIButton *button = [scrollView valueForKeyPath:@"emptyDataSetView.button"];
    if (button) {
        // Change button width
        for (NSLayoutConstraint *constraint in button.superview.constraints) {
            if (constraint.firstItem == button && constraint.firstAttribute == NSLayoutAttributeLeading) {
                constraint.constant = 130.0;
            } else if (constraint.secondItem == button && constraint.secondAttribute == NSLayoutAttributeTrailing) {
                constraint.constant = 130.0;
            }
        }
        // Change button height
        for (NSLayoutConstraint *constraint in button.constraints) {
            if (constraint.firstItem == button && constraint.firstAttribute == NSLayoutAttributeHeight) {
                constraint.constant = 40.0;
            }
        }
    }
}

xiaopin avatar May 29 '18 09:05 xiaopin