UIAlertController 是iOS8 之后推出的!实际使用中我们遇到连个AlertAction 颜色不一样的问题!
下面给出怎么更改系统的UIAlertController AlertAction字体颜色
修改标题的内容,字号,颜色。使用的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 修改字体颜色的关键字
[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);
通过修改枚举值 UIAlertControllerStyleActionSheet 弹出sheet
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
很多时候我们都会用到pop视图 iOS 8之后苹果出UIPopoverPresentationController 直接上栗子
1. 点击导航栏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];
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.实现代理方法
//代理方法需要的实现方式
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}
// 是否可以dismiss,返回YES代表可以,返回NO代表不可以(点击空白区域)
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
return YES;
}
最后献上demo下载