標籤:uialertcontroller ios8
昨天工程裡需要將 UIActionSheet 中字型的顏色統一為系統的藍色,所以便去網上找辦法。
辦法很簡單,遍曆出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];
// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(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; // before animation and showing view
- (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 再改變屬性。
當你這樣相等之後,兩個按鈕其實佔有的是同一個指標,所以可以改變其中按鈕的屬性。當然你也可以直接用subView(UIAlertButton)改變屬性。
這樣之後,便可以改變字型中顏色了。
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之後的UIAlertController