UIAlertController

效果圖

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下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容