
效果圖
UIAlertController 是iOS8 之后推出的!實際使用中我們遇到連個AlertAction 顏色不一樣的問題!
下面給出怎么更改系統(tǒng)的UIAlertController AlertAction字體顏色
修改標(biāo)題的內(nèi)容,字號,顏色。使用的key值是“attributedTitle”
NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:@"你好嗎"];
[str addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:18],NSForegroundColorAttributeName : [UIColor redColor]}
NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:@"你好嗎"];
[str addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:18],NSForegroundColorAttributeName : [UIColor redColor]} range:NSMakeRange(0, [str string].length)];
[alertVC setValue:str forKey:@"attributedTitle"];
修改AlertAction 字體顏色
_titleTextColor 修改字體顏色的關(guān)鍵字
[ok setValue:[UIColor purpleColor] forKey:@"_titleTextColor"];
[cancel setValue:[UIColor orangeColor] forKey:@"_titleTextColor"];

修改字體顏色效果圖
實際使用中會用到輸入文字 如登錄
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"登錄";
}];
[alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"密碼";
}];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"登錄" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"登錄 = %@",[alertVC.textFields firstObject].text);
NSLog(@"密碼 = %@",[alertVC.textFields lastObject].text);
}];
然后通過textFields 這個屬性來獲取輸入的文字
NSLog(@"登錄 = %@",[alertVC.textFields firstObject].text);
NSLog(@"密碼 = %@",[alertVC.textFields lastObject].text);

帶輸入框alert
通過修改枚舉值 UIAlertControllerStyleActionSheet 彈出sheet
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertControllerStyleActionSheet
很多時候我們都會用到pop視圖 iOS 8之后蘋果出UIPopoverPresentationController 直接上栗子
1. 點擊導(dǎo)航欄item
PopViewController *popViewController = [[PopViewController alloc] init];
popViewController.modalPresentationStyle = UIModalPresentationPopover;
popViewController.popoverPresentationController.barButtonItem = sender;
popViewController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
popViewController.popoverPresentationController.backgroundColor = popViewController.view.backgroundColor;
popViewController.preferredContentSize = CGSizeMake(100, 150);
popViewController.popoverPresentationController.delegate = popViewController;
[self presentViewController:popViewController animated:YES completion:NULL];

點擊item彈出效果圖
1. 點擊自定義按鈕
PopViewController *popViewController = [[PopViewController alloc] init];
popViewController.modalPresentationStyle = UIModalPresentationPopover;
popViewController.popoverPresentationController.sourceView = self.popBtn;
popViewController.popoverPresentationController.sourceRect = self.popBtn.bounds;
popViewController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
popViewController.popoverPresentationController.backgroundColor = popViewController.view.backgroundColor;
popViewController.preferredContentSize = CGSizeMake(100, 150);
popViewController.popoverPresentationController.delegate = popViewController;
[self presentViewController:popViewController animated:YES completion:NULL];

點擊自定義按鈕效果圖
3.實現(xiàn)代理方法
//代理方法需要的實現(xiàn)方式
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}
// 是否可以dismiss,返回YES代表可以,返回NO代表不可以(點擊空白區(qū)域)
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
return YES;
}
最后獻(xiàn)上demo下載