今天适配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];
}
}
}
看起来没有问题,但是实际运用中会发现遍历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;
}
//想要看详细代码 ---点我