适配ios7的时候仿UIAlertView弹框效果,修改内部的label和button的属性
2016-07-22 22:01
531 查看
今天适配ios7的时候,需要有一个弹框的效果
在xcode8(ios10)还没有出来的现在,还得苦逼的适配ios7,里面就有一些让人无语的坑,其中弹框效果就是一个,对于想要实现自定义弹框的效果是需要花上一定的功夫的 如下图:
在ios7中不能使用UIAlertViewController来进行弹框,基本上更改UIAlertViewController的弹框的属性是比较简单的,网上的方法很多。但这次我是要实现在ios7上出现弹框效果,并且更改中间的messgaeLabel的文字颜色。经过研究,我在网上找到了一种方法:但是并没有解决我的问题
方法如下:
- (IBAction)showBigAlert:(id)sender {
UIAlertView* find = [[UIAlertView alloc] initWithTitle:@"big
size view" message:@"do you see it"delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[find show];
[find release];
}
-(void)willPresentAlertView:(UIAlertView *)alertView {
[alertView setFrame:CGRectMake(1, 20, 1000, 600)];
for( UIView * view in alertView.subviews )
{
if( [view isKindOfClass:[UILabel class]] )
{
UILabel* label = (UILabel*) view;
label.textAlignment = UITextAlignmentLeft;
label.font=[UIFont fontWithName:@"STHeitiSC-Medium" size:18];
label.textColor=[UIColor greenColor];
}
}
}
-(instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle
idenfyButtonTitle:(NSString *)idenfyButtonTitle hasBtnCount:(int)num superView:(UIView *)superView{
//弹出的view
self=[super
init];
//遮罩视图(父view)
self.faV=[[UIView
alloc] initWithFrame:[UIScreen
mainScreen].bounds];
self.faV.backgroundColor=[UIColor
blackColor];
self.faV.alpha=0.4;
//MARK:
弹出alertView
//设置frame
CGFloat marginX =
5*ten;
CGFloat height =
13.5*ten;
self.backgroundColor=[UIColor
whiteColor];
self.frame=CGRectMake(marginX,([UIScreen
mainScreen].bounds.size.height-height)/2 ,[UIScreen
mainScreen].bounds.size.width -
2*marginX, height);
self.layer.cornerRadius =
ten;
self.layer.masksToBounds=YES;
//设置子控件
UIButton *cancleBt=[self
addBtntag:0
title:cancelButtonTitle
alertView:self
hasBtnCount:2];
UIButton *idenfyBt=[self
addBtntag:1
title:idenfyButtonTitle
alertView:self
hasBtnCount:2];
//添加label
CGFloat xM=ten;
CGFloat yM=2*ten;
UILabel *titleLab=[[UILabel
alloc]initWithFrame:CGRectMake(xM, yM,
self.frame.size.width-2*xM,
4*ten)];
titleLab.textColor=[UIColor
blackColor];
titleLab.font=[UIFont
systemFontOfSize:1.6*ten];
titleLab.numberOfLines=0;
titleLab.text=title;
titleLab.textAlignment=NSTextAlignmentCenter;
UILabel *messageLab=[[UILabel
alloc]initWithFrame:CGRectMake(xM, yM+titleLab.frame.size.height-ten,
self.frame.size.width-2*xM,
4*ten)];
messageLab.text=message;
messageLab.textAlignment=NSTextAlignmentCenter;
messageLab.textColor=[UIColor
redColor];
messageLab.font=[UIFont
systemFontOfSize:1.6*ten];
//添加
[self addSubview:cancleBt];
[self addSubview:idenfyBt];
[self addSubview:titleLab];
[self addSubview:messageLab];
[superView addSubview:self.faV];
return
self;
}
//想要看详细代码 ---点我
在xcode8(ios10)还没有出来的现在,还得苦逼的适配ios7,里面就有一些让人无语的坑,其中弹框效果就是一个,对于想要实现自定义弹框的效果是需要花上一定的功夫的 如下图:
在ios7中不能使用UIAlertViewController来进行弹框,基本上更改UIAlertViewController的弹框的属性是比较简单的,网上的方法很多。但这次我是要实现在ios7上出现弹框效果,并且更改中间的messgaeLabel的文字颜色。经过研究,我在网上找到了一种方法:但是并没有解决我的问题
方法如下:
- (IBAction)showBigAlert:(id)sender {
UIAlertView* find = [[UIAlertView alloc] initWithTitle:@"big
size view" message:@"do you see it"delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[find show];
[find release];
}
-(void)willPresentAlertView:(UIAlertView *)alertView {
[alertView setFrame:CGRectMake(1, 20, 1000, 600)];
for( UIView * view in alertView.subviews )
{
if( [view isKindOfClass:[UILabel class]] )
{
UILabel* label = (UILabel*) view;
label.textAlignment = UITextAlignmentLeft;
label.font=[UIFont fontWithName:@"STHeitiSC-Medium" size:18];
label.textColor=[UIColor greenColor];
}
}
}
看起来没有问题,但是实际运用中会发现遍历alertView.subviews的时候不会进入遍历方法,打印alertView.subviews的时候值也为空,因此没有办法,只能在重新研究,发现网上也有些同行们遇到了这个问题,值为空。
找老大询问也没有得到合理的解释,因此无奈只能放弃遍历alertView.subviews的办法来更改其属性的值,选择用自定义的控件来实现alertView的弹框效果。
此处如果有大神知道怎么直接用系统的alertView实现更改字体的颜色,非常希望来赐教!!
我做了个小demo(还不是很完全,刚刚抽了点时间写的,先完成项目),上传到git上,有兴趣的可以去下载。
代码如下:-(instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle
idenfyButtonTitle:(NSString *)idenfyButtonTitle hasBtnCount:(int)num superView:(UIView *)superView{
//弹出的view
self=[super
init];
//遮罩视图(父view)
self.faV=[[UIView
alloc] initWithFrame:[UIScreen
mainScreen].bounds];
self.faV.backgroundColor=[UIColor
blackColor];
self.faV.alpha=0.4;
//MARK:
弹出alertView
//设置frame
CGFloat marginX =
5*ten;
CGFloat height =
13.5*ten;
self.backgroundColor=[UIColor
whiteColor];
self.frame=CGRectMake(marginX,([UIScreen
mainScreen].bounds.size.height-height)/2 ,[UIScreen
mainScreen].bounds.size.width -
2*marginX, height);
self.layer.cornerRadius =
ten;
self.layer.masksToBounds=YES;
//设置子控件
UIButton *cancleBt=[self
addBtntag:0
title:cancelButtonTitle
alertView:self
hasBtnCount:2];
UIButton *idenfyBt=[self
addBtntag:1
title:idenfyButtonTitle
alertView:self
hasBtnCount:2];
//添加label
CGFloat xM=ten;
CGFloat yM=2*ten;
UILabel *titleLab=[[UILabel
alloc]initWithFrame:CGRectMake(xM, yM,
self.frame.size.width-2*xM,
4*ten)];
titleLab.textColor=[UIColor
blackColor];
titleLab.font=[UIFont
systemFontOfSize:1.6*ten];
titleLab.numberOfLines=0;
titleLab.text=title;
titleLab.textAlignment=NSTextAlignmentCenter;
UILabel *messageLab=[[UILabel
alloc]initWithFrame:CGRectMake(xM, yM+titleLab.frame.size.height-ten,
self.frame.size.width-2*xM,
4*ten)];
messageLab.text=message;
messageLab.textAlignment=NSTextAlignmentCenter;
messageLab.textColor=[UIColor
redColor];
messageLab.font=[UIFont
systemFontOfSize:1.6*ten];
//添加
[self addSubview:cancleBt];
[self addSubview:idenfyBt];
[self addSubview:titleLab];
[self addSubview:messageLab];
[superView addSubview:self.faV];
return
self;
}
//想要看详细代码 ---点我
相关文章推荐
- UICollectionViewCell 3种创建方法
- Biee 迁移和刷新GUIDs
- GUI为什么不设计为多线程(用户事件和底层事件的流程是相反的,每层都加锁效率太低,共用一把锁那就是单线程)
- codeforces 691E Xor-sequences(矩阵快速幂)
- 【POJ 2533】Longest Ordered Subsequence(最长上升子序列LIS)
- cpuinfo和lscpu查看CPU相关的信息
- EasyUI ComboBox(下拉列表框)
- POJ 1947 Rebuilding Roads (树形dp + 01背包)
- UICollectionViewCell中cell的定义与自定义方式总结
- 获取Map中的所有value
- Handler,Looper,MessageQueue,ThreadLocal讲解以及实例
- Pin 3.0 User Guide
- iOS-UI之UICollectionView must be initialized with a non-nil layout parameter'解决方式
- SPOJ RMQSQ Range Minimum Query 《RMQ》
- 基于MVC+EasyUI的Web开发框架经验总结(10)--在Web界面上实现数据的导入和导出
- 基于MVC+EasyUI的Web开发框架经验总结(9)--在Datagrid里面实现外键字段的转义操作
- 基于MVC+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览
- 基于MVC+EasyUI的Web开发框架经验总结(7)--实现省份、城市、行政区三者联动
- 128. Longest Consecutive Sequence
- hdoj 2818 Building Block(加权并查集)