自定义UIView主要内容就是重写几个继承下来的几个方法,把自己想要的东西放进去,布局好即可,由于我这个控件比较简单,所以我没有自定义模型数据。
以下代码拷贝到项目中,调用运行即可。
WITLoadingView.h
//
// WITLoadingView.h
// UIActivityIndicatorViewTest
//
// Created by witsystem on 2018/3/29.
// Copyright © 2018年 witsystem. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface WITLoadingView : UIView
@end
WITLoadingView.m
//
// WITLoadingView.m
// UIActivityIndicatorViewTest
//
// Created by witsystem on 2018/3/29.
// Copyright © 2018年 witsystem. All rights reserved.
//
#import "WITLoadingView.h"
@interface WITLoadingView ()
@property (nonatomic, weak) UIActivityIndicatorView *activityIndicatorView;
@property (nonatomic, weak) UILabel *markLabel;
@end
@implementation WITLoadingView
-(instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray];
self.activityIndicatorView = aiv;
[aiv startAnimating];
[self addSubview:aiv];
UILabel *label = [[UILabel alloc] init];
label.text = @"正在加载请稍后...";
self.markLabel = label;
[self addSubview: label];
self.markLabel.textColor = [UIColor grayColor];
self.backgroundColor = [UIColor colorWithRed: 240/255.0 green:240/255.0 blue: 240/255.0 alpha: 0.5];
}
return self;
}
-(void)layoutSubviews {
[super layoutSubviews];
CGFloat mlW = 120;
CGFloat mlH = 20;
CGFloat aivW = 20;
CGFloat aivH = 20;
CGFloat parentW = [UIScreen mainScreen].bounds.size.width;
CGFloat parentH = [UIScreen mainScreen].bounds.size.height;
//控件间隔宽度
CGFloat intervalW = 10;
CGFloat aivX = parentW * 0.5 - mlW * 0.5 - aivW * 0.5;
CGFloat aivY = parentH * 0.5 - aivW * 0.5;
CGFloat mlX = aivX + aivW + intervalW;
CGFloat mlY = aivY;
self.activityIndicatorView.frame = CGRectMake(aivX, aivY, aivW, aivH);
self.markLabel.frame = CGRectMake(mlX, mlY, mlW, mlH);
self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}
@end
调用方法
#import "WITLoadingView.h"
WITLoadingView *loadingView = [[WITLoadingView alloc] init];
[self.view.window addSubview: loadingView];
隐藏等待控件
[loadingView removeFromSuperview];