适配ios7的时候仿UIAlertView弹框效果,修改内部的label和button的属性

今天适配ios7的时候,需要有一个弹框的效果 

在xcode8(ios10)还没有出来的现在,还得苦逼的适配ios7,里面就有一些让人无语的坑,其中弹框效果就是一个,对于想要实现自定义弹框的效果是需要花上一定的功夫的    如下图:


  在ios7中不能使用UIAlertViewController来进行弹框,基本上更改UIAlertViewController的弹框的属性是比较简单的,网上的方法很多。但这次我是要实现在ios7上出现弹框效果,并且更改中间的messgaeLabel的文字颜色。经过研究,我在网上找到了一种方法:但是并没有解决我的问题


方法如下:

- (IBAction)showBigAlert:(id)sender {

     UIAlertView* find = [[UIAlertView allocinitWithTitle:@"big size view" message:@"do you see it"delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nilnil];

 

    [find show];

    [find release];

    

}

-(void)willPresentAlertView:(UIAlertView *)alertView {

    [alertView setFrame:CGRectMake(1201000600)]; 

    forUIView * 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;

}


//想要看详细代码 ---点我




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义UIAlertView的方法已经在iOS13之后被废弃,推荐使用UIAlertController来代替。下面介绍如何自定义UIAlertController的弹出位置以及宽度。 1. 自定义弹出位置 可以使用UIAlertController的popoverPresentationController属性来设置弹出位置。具体步骤如下: ``` // 创建UIAlertController对象 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; // 创建弹出位置的UIView对象 UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)]; // 设置弹出位置 alertController.popoverPresentationController.sourceView = popoverView; alertController.popoverPresentationController.sourceRect = popoverView.bounds; // 弹出UIAlertController [self presentViewController:alertController animated:YES completion:nil]; ``` 2. 自定义宽度 可以通过设置UIAlertController的preferredContentSize属性来改变其宽度。具体步骤如下: ``` // 创建UIAlertController对象 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; // 设置宽度 alertController.preferredContentSize = CGSizeMake(280, 200); // 弹出UIAlertController [self presentViewController:alertController animated:YES completion:nil]; ``` 需要注意的是,此方法只适用于UIAlertControllerStyleAlert风格的UIAlertController。如果使用UIAlertControllerStyleActionSheet风格的UIAlertController,设置preferredContentSize属性将不会生效。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值