iOS 封装全局提示框
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 封装全局提示框相关的知识,希望对你有一定的参考价值。
很多时候产品都有需求, 要提示框, 提示框提示框!!! 系统的太难看了, (ーー゛)我勒个. 让UI给个标准吧我做o(╯□╰)o
其实很简单 可以根据要求让它以各种动画形式出现消失.
command+ n 创建UIView 的类 ,命名为 AWPopView
AWPopView.h文件
//取消按钮点击事件
typedef void(^cancelBlock)();
//确定按钮点击事件
typedef void(^sureBlock)();
@interface AWPopView : UIView
@property(nonatomic,copy)cancelBlock cancel_block;
@property(nonatomic,copy)sureBlock sure_block;
/**
* 简书号:ios_啊伟 http://www.cnblogs.com/chenwei-dcav/
*
* @param title 标题
* @param content 内容
* @param sure 确定按钮内容
* @param sureBlock 确定按钮点击事件
*
* @return SZKAlterView
*/
+(instancetype)alterViewWithTitle:(NSString *)title
content:(NSString *)content
sure:(NSString *)sure
sureBtClcik:(sureBlock)sureBlock;
AWPopView.m文件
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface AWPopView()
@property(nonatomic,retain)UIView *alterView;
@property(nonatomic,retain)UILabel *titleLb;
@property(nonatomic,retain)UILabel *contentLb;
@property(nonatomic,retain)UIButton *sureBt;
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *content;
@property(nonatomic,copy)NSString *cancel;
@property(nonatomic,copy)NSString *sure;
@end
@implementation AWPopView
-(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self) {
//标题
_titleLb=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 15)];
_titleLb.textColor=SetColor(111, 208, 199);
_titleLb.font = [UIFont systemFontOfSize:16];
_titleLb.adjustsFontForContentSizeCategory = YES;
[self addSubview:_titleLb];
_titleLb.sd_layout.leftSpaceToView(self, 22.5).topSpaceToView(self, 15).widthIs(70).heightIs(15);
//内容
_contentLb=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_titleLb.frame) + 30, self.bounds.size.width, 15)];
_contentLb.font = [UIFont systemFontOfSize:15];
[self addSubview:_contentLb];
_contentLb.sd_layout.leftSpaceToView(self, 22.5).topSpaceToView(_titleLb, 30).widthIs(self.bounds.size.width).heightIs(15);
//确定按钮
_sureBt=[[UIButton alloc]initWithFrame:CGRectMake(self.bounds.size.width/2, CGRectGetMaxY(_contentLb.frame), 120, 40)];
_sureBt.centerX = self.centerX;
_sureBt.layer.cornerRadius = 5;
_sureBt.layer.masksToBounds = YES;
_sureBt.backgroundColor = SetColor(111, 208, 199);
[_sureBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_sureBt addTarget:self action:@selector(sureBtClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_sureBt];
_sureBt.sd_layout.leftSpaceToView(self, (self.bounds.size.width - 120)/2).bottomSpaceToView(self, 20).widthIs(120).heightIs(40);
}
return self;
}
#pragma mark----实现类方法
+(instancetype)alterViewWithTitle:(NSString *)title
content:(NSString *)content
sure:(NSString *)sure
sureBtClcik:(sureBlock)sureBlock;
{
AWPopView *alterView=[[AWPopView alloc]initWithFrame:CGRectMake(0, 0, 345, 200)];
alterView.backgroundColor=[UIColor whiteColor];
alterView.center=CGPointMake(WIDTH/2, HEIGHT/2 +20);
alterView.layer.cornerRadius=5;
alterView.layer.masksToBounds=YES;
alterView.title=title;
alterView.content=content;
alterView.sure=sure;
alterView.sure_block=sureBlock;
return alterView;
}
#pragma mark--给属性重新赋值
-(void)setTitle:(NSString *)title
{
_titleLb.text=title;
}
-(void)setContent:(NSString *)content
{
_contentLb.text=content;
}
-(void)setSure:(NSString *)sure
{
[_sureBt setTitle:sure forState:UIControlStateNormal];
}
#pragma mark----确定按钮点击事件
-(void)sureBtClick
{
[self removeFromSuperview];
self.sure_block();
}
类也写完了 , 激动人心的时刻到了, 让他再任何ViewController 出现消失
ViewController.m 文件
//添加淡黑色背景, 弹框出现其他地方不能点击
UIWindow * window = [[UIApplication sharedApplication]keyWindow];
UIView * BlackView = [[UIView alloc]init];
BlackView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
[window addSubview:BlackView];
BlackView.sd_layout.leftSpaceToView(window, 0).topSpaceToView(window, 0).rightSpaceToView(window, 0).bottomSpaceToView(window, 0);//做好适配
AWPopView *lll=[AWPopView alterViewWithTitle:@"系统提示" content:@"需要重新登录" sure:@"确定" sureBtClcik:^{
BlackView.alpha = 0.0f;
LoginViewController * login = [[LoginViewController alloc]init];
[self.navigationController pushViewController:login animated:YES];
}];
[window addSubview:lll];
直接类方法创建, 就是这么sou eaey . 弹框里面的按钮标题可以去类里面随便加 , 如果觉得不够过瘾的iOS 妹子可以加QQ 751045314 ,阿伟不介意我们深入沟通哦,要demo留下QQ发邮箱
以上是关于iOS 封装全局提示框的主要内容,如果未能解决你的问题,请参考以下文章