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

至此问题完美解决。

相关文章:

  • 2021-08-29
  • 2021-09-11
  • 2021-09-14
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-04
  • 2022-12-23
  • 2021-10-27
  • 2021-07-20
相关资源
相似解决方案