美文网首页
MBProgressHUD自定义格式,以及添加自定义图片

MBProgressHUD自定义格式,以及添加自定义图片

作者: 小小程序媛之路 | 来源:发表于2017-08-25 18:36 被阅读762次
WechatIMG29.jpeg

假如你拿到这样的设计图,你第一反应是什么呢?是不是觉得没什么特别啊,就一个普普通通的view,实现起来也不会有什么难啊!老实说,我也是这样想的,可是我的组长一定要我用MBProgressHUD去实现哦。妈妈咪,这不是为难我吗?本来我对MBProgressHUD的实现原因就不了解,还要我去改它的第三方库,不过也是,正因为我不理解,让我去改改就会了解。

1.弹框离界面的左右边距192px的实现

这个我看了MBProgressHUD好久,一直不知道怎样去改,因为- (void)updateConstraints方法中的NSLayoutConstraint *padding = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:subviews[idx - 1] attribute:NSLayoutAttributeBottom multiplier:1.f constant:0.f];这些,我真的看不懂,不过好彩尝试通过给detailLabel添加约束。我这个主要是要限定最大的宽度的,所以采用以下判断来布局。

CGFloat textWidth = [msg boundingRectWithSize:CGSizeMake(MAXFLOAT, 33) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil].size.width;
    
if (textWidth > (SCREEN_WIDTH - 100 - 40)) {
    [hud.detailsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
         make.width.equalTo(@(SCREEN_WIDTH - 100 - 40));
    }];
}

实质是黑色的弹框的大小是依据中间的detailsLabel的宽度来做约束的,所以你只要限制中间detailsLabel的宽度就可以了。

2.图片大小的限定

首先,要实现图片的显示,MBProgressHUD的mode要设置为MBProgressHUDModeCustomView。要限定图片大小,有以下两种方法:
1.MBProgressHUD是根据你给定的图片大小来约束大小,所以你要图片显示多大,你就叫UI给你这种尺寸的切图就可以了。
2.自己通过代码缩小图片

#pragma mark - 等比例去压缩图片大小
- (UIImage *)scaleImage:(UIImage *)originImage toWidth:(CGFloat)width
{
     if (width > originImage.size.width) {
            return originImage;
    }

    CGFloat height = (width / originImage.size.width) * originImage.size.height;

    CGRect  rect = CGRectMake(0, 0, width, height);

    UIGraphicsBeginImageContext(rect.size);

    [originImage drawInRect:rect];

    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;

}

3.用MBProgressHUD做自定义动画

hud.mode = MBProgressHUDModeCustomView;
UIImageView *gifImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gif_loading_m_1"]];
NSMutableArray *arrOrigin = [[NSMutableArray alloc] initWithObjects:
                                   [UIImage imageNamed:@"gif_loading_m_1"],
                                   [UIImage imageNamed:@"gif_loading_m_2"],
                                   [UIImage imageNamed:@"gif_loading_m_3"],
                                   [UIImage imageNamed:@"gif_loading_m_4"],
                                   [UIImage imageNamed:@"gif_loading_m_5"],
                                   [UIImage imageNamed:@"gif_loading_m_6"],
                                   [UIImage imageNamed:@"gif_loading_m_7"],
                                   [UIImage imageNamed:@"gif_loading_m_8"],
                                   [UIImage imageNamed:@"gif_loading_m_3"],
                                   [UIImage imageNamed:@"gif_loading_m_1"],
                                   nil];

[gifImageView setAnimationImages:arrOrigin];
[gifImageView setAnimationDuration:1];
[gifImageView setAnimationRepeatCount:0];
[gifImageView startAnimating];

hud.customView = gifImageView;

相关文章

网友评论

      本文标题:MBProgressHUD自定义格式,以及添加自定义图片

      本文链接:https://www.haomeiwen.com/subject/wtkpdxtx.html