自定义UIActionSheet的颜色IOS8前后的区别

来源:互联网 发布:汽车配件管理销售软件 编辑:程序博客网 时间:2024/06/06 22:00



IOS8之后的做法:


在IOS8之后UIActionSheet变成了UIAlertController,官方是这样说的

一个UIAlertController对象向用户显示一个警告信息。这类取代UIActionSheet和UIAlertView类显示警报。用行动和你想要的风格配置警报控制器后,目前使用的presentviewcontroller:动画:完成:方法。

(现在已经是一个控制器了 区别于以前 现在继承于 NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController

以前:NS_CLASS_AVAILABLE_IOS(2_0) @interface UIActionSheet : UIView 

ios8之后 你需要这样创建,当然,你用以前的方法创建的在ios8上同样可以显示,点击等等,只不过有些方法就没用了,例如本文中修改字体的方法,因为不再是 UIView 而是 UIViewController ,而且要注意一点的是,当你用下述 ios8之后创建的方法,跑在ios8的机子上,代理是不会再执行的,不知道对不对,不过我跑的时候 上面两个代理方法就没有执行。。。

重要:UIActionSheet在iOS 8不赞成。(注意,UIActionSheetdelegate也是过时的。)来创建和管理动作片在iOS 8之后,转而使用UIAlertController与preferredstyle的UIAlertControllerstyleactionsheet。

在应用程序的目标版本的iOS到iOS 8之前,使用UIActionSheet类向用户显示一组选择如何进行一个给定的任务。你也可以使用动作片提示用户确认潜在危险的行动。动作片包含一个可选的标题和一个或多个按钮,每一个对应于所采取的行动。

使用该类的属性和方法来配置动作片的消息,风格,和之前提交按钮。你也应该指定一个代表你的动作片。你的委托对象负责执行与任何按钮时,他们被窃听和应符合uiactionsheetdelegate协议的行动。有关实现委托的方法的更多信息,见UIActionSheetdelegate协议参考。

            UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

            [alertCtrl addAction:[UIAlertAction actionWithTitle:@"取消"

                                                          style:UIAlertActionStyleCancel

                                                        handler:^(UIAlertAction *action) {

                                                            //code

[self presentViewController:alertCtrl animated:YES completion:nil];

或者你可以


    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

    

    // Create the actions.

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        NSLog(@"The "Okay/Cancel" alert's cancel action occured.");

    }];


[alertController addAction:cancelAction];  

[self presentViewController:alertController animated:YES completion:nil];


其中,几个属性不用说你也应该知道

typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {

    UIAlertControllerStyleActionSheet = 0,

    UIAlertControllerStyleAlert

NS_ENUM_AVAILABLE_IOS(8_0);


typedef NS_ENUM(NSInteger, UIAlertActionStyle) {

    UIAlertActionStyleDefault = 0,

    UIAlertActionStyleCancel,

    UIAlertActionStyleDestructive

NS_ENUM_AVAILABLE_IOS(8_0);


在这里,如何修改颜色字体呢 我想可能是  UIAlertController 的这个方法吧。。

- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;

在这里 我直接用了UIAlertActionStyleDestructive 把其中的某项改成UIAlert的红色,所以没有真正自定义颜色,不过去了解一下估计就可以了,还是挺简单的。




IOS8之前的做法


办法很简单,遍历出UIActionSheet中的子控件。当然 这是ios8之前的方法,为什么这么说呢,等下你就知道了,先说说ios8之前的调用吧。

IOS8之前

创建一个UIActionSheet

           UIActionSheet *t_actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拨号" otherButtonTitles:@"发送短信", @"复制名片", nil];

            t_actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;

            [t_actionSheet showInView:self.view];


-(void)actionSheet:(UIActionSheet *)actionSheet clickeonAtIndex:(NSInteger)buttonIndex

{

    if (buttonIndex == 0) {

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",_mar_num[_selectRow][1]]]];

    }else if (buttonIndex == 1) {

        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"sms://%@",_mar_num[_selectRow][1]]]];

    }else if(buttonIndex == 2) {

        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

        pasteboard.string = [NSString stringWithFormat:@"%@:%@",_mar_num[_selectRow][2],_mar_num[_selectRow][1]];

    }

}


- (void)willPresentActionSheet:(UIActionSheet *)actionSheet

{

    for (id subViwe in actionSheet.subviews) {

        if ([subViwe isKindOfClass:[UIButton class]]) {

            UIButton *button = (UIButton*)subViwe;

            [button setTitleColor:[UIColor colorWithRed:0 green:0.478431 blue:1 alpha:1] forState:UIControlStateNormal];

        }

    }

}

打印后你可以清晰的看到,在即将出现的UIActionSheet 里面是有5个子视图的,其中4个按钮都为我自己添加,(第0个可能是背景颜色吧、没有去试过)然后你可以看代码,取出之后赋给一个 UIButton 再改变属性。




IOS8之前的做法


办法很简单,遍历出UIActionSheet中的子控件。当然 这是ios8之前的方法,为什么这么说呢,等下你就知道了,先说说ios8之前的调用吧。

IOS8之前

创建一个UIActionSheet

           UIActionSheet *t_actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拨号" otherButtonTitles:@"发送短信", @"复制名片", nil];

            t_actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;

            [t_actionSheet showInView:self.view];


-(void)actionSheet:(UIActionSheet *)actionSheet clickeonAtIndex:(NSInteger)buttonIndex

{

    if (buttonIndex == 0) {

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",_mar_num[_selectRow][1]]]];

    }else if (buttonIndex == 1) {

        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"sms://%@",_mar_num[_selectRow][1]]]];

    }else if(buttonIndex == 2) {

        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

        pasteboard.string = [NSString stringWithFormat:@"%@:%@",_mar_num[_selectRow][2],_mar_num[_selectRow][1]];

    }

}


- (void)willPresentActionSheet:(UIActionSheet *)actionSheet

{

    for (id subViwe in actionSheet.subviews) {

        if ([subViwe isKindOfClass:[UIButton class]]) {

            UIButton *button = (UIButton*)subViwe;

            [button setTitleColor:[UIColor colorWithRed:0 green:0.478431 blue:1 alpha:1] forState:UIControlStateNormal];

        }

    }

}

打印后你可以清晰的看到,在即将出现的UIActionSheet 里面是有5个子视图的,其中4个按钮都为我自己添加,(第0个可能是背景颜色吧、没有去试过)然后你可以看代码,取出之后赋给一个 UIButton 再改变属性。

0 0
原创粉丝点击