MBProgressHUD自定义customView

MBProgressHUD 自定义customView 比较简单代码如下:

MBProgressHUD _customHud = [[MBProgressHUD alloc]initWithFrame:MAIN_SCREEN_BOUNDS]; 
_customHud.margin
= 0;
_customHud.customView
= self.noticeView;
_customHud.mode
= MBProgressHUDModeCustomView;

但是你很快就会发现自定义view的frame值不管你怎么设置,都不对,都不是想要的结果上github上,查了一下得到一下结果:

1.首先自定义view必须重写:intrinsicContentSize方法

2.手动设置 translatesAutoresizingMaskIntoConstraints = NO

完整代码如下:

#import "GLNoticeView.h"

@implementation GLNoticeView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (CGSize)intrinsicContentSize {
    CGFloat contentViewH = 200;
    CGFloat contentViewW = MAIN_SCREEN_WIDTH - 40;
    return CGSizeMake(contentViewW, contentViewH);
}


- (instancetype)initWithFrame:(CGRect)frame {
   self = [[NSBundle mainBundle] loadNibNamed:@"GLNoticeView" owner:nil options:nil].firstObject;
    self.translatesAutoresizingMaskIntoConstraints = NO;
    self.layer.cornerRadius = 4;
    self.layer.masksToBounds = YES;
    return self;
}

@end

至此问题完美解决。

原文地址:https://www.cnblogs.com/TengSys/p/6739121.html