我正在使用新的UIAlertController来显示警报 . 我有这个代码:
// nil titles break alert interface on iOS 8.0, so we'll be using empty strings
UIAlertController *alert = [UIAlertController alertControllerWithTitle: title == nil ? @"": title message: message preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle: cancelButtonTitle style: UIAlertActionStyleCancel handler: nil];
[alert addAction: defaultAction];
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootViewController presentViewController:alert animated:YES completion:nil];
现在我想更改 Headers 和消息字体,颜色,大小等 . 什么是最好的方法呢?
Edit: 我应该插入整个代码 . 我为UIView创建了一个类别,我可以为iOS版本显示正确的警报 .
@implementation UIView (AlertCompatibility)
+( void )showSimpleAlertWithTitle:( NSString * )title
message:( NSString * )message
cancelButtonTitle:( NSString * )cancelButtonTitle
{
float iOSVersion = [[UIDevice currentDevice].systemVersion floatValue];
if (iOSVersion < 8.0f)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: title
message: message
delegate: nil
cancelButtonTitle: cancelButtonTitle
otherButtonTitles: nil];
[alert show];
}
else
{
// nil titles break alert interface on iOS 8.0, so we'll be using empty strings
UIAlertController *alert = [UIAlertController alertControllerWithTitle: title == nil ? @"": title
message: message
preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle: cancelButtonTitle
style: UIAlertActionStyleCancel
handler: nil];
[alert addAction: defaultAction];
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootViewController presentViewController:alert animated:YES completion:nil];
}
}
22 回答
不确定这是针对私有API /属性,而是使用KVC在ios8上为我工作
对于记录,也可以使用这些私有API更改警报操作的字体 . 再次,它可能会让你的应用被拒绝,我还没有尝试提交这样的代码 .
对于XCode 10中的Swift 4.2,现在最后两行是:
您可以通过将色调颜色应用于UIAlertController来更改按钮颜色 .
在iOS 9上,如果窗口色调颜色设置为自定义颜色,则必须在显示警报后立即应用色调颜色 . 否则,色调颜色将重置为您的自定义窗口色调颜色 .
您可以使用以下代码更改按钮文本的颜色:
也许这会对你有所帮助 .
Output
@ dupuis2387答案的快速翻译 . 算法使用
attributedTitle
键通过KVC设置UIAlertController
Headers 的颜色和字体 .使用
UIAppearance
协议 . 设置字体的示例 - 创建要扩展的类别UILabel
:它的用法:
测试并使用样式
UIAlertControllerStyleActionSheet
,但我想它也适用于UIAlertControllerStyleAlert
.附:更好地检查类可用性而不是iOS版本:
使用
UIAppearance
协议 . 使用appearanceFont
进行更多修改以更改UIAlertAction
的字体 .为
UILabel
创建一个类别UILabel FontAppearance.h
UILabel FontAppearance.m
用法:
加
[[UILabel appearanceWhenContainedIn:UIAlertController.class, nil] setAppearanceFont:nil];
在
AppDelegate.m
中使其适用于所有UIAlertController
.我正在使用它 .
添加一行(AppDelegate)并适用于所有UIAlertController .
Swift 4
Headers 上的自定义字体示例 . 消息或操作等其他组件也是如此 .
在呈现后在视图上设置色调颜色存在问题;即使你在presentViewController:animated:completion:的完成块中执行它,它也会对按钮 Headers 的颜色产生闪烁效果 . 这是草率的,不专业的,完全不可接受的 .
提出的其他解决方案取决于视图层次结构保持静态,这是Apple不愿意做的事情 . 预计这些解决方案将在未来的iOS版本中失败 .
解决这个问题并在任何地方进行的一种确定的方法是通过向UIAlertController添加一个类别并调用viewWillAppear .
Headers :
实施:
将.pch(预编译头)添加到项目中并包含以下类别:
确保在项目中正确注册pch,并且它将在每个使用UIAlertController的类中包含类别方法 .
然后,在你的app代理中使用didFinishLaunchingWithOptions方法,导入你的类别并调用
并且它会自动传播到您应用程序中的每个UIAlertController实例,无论是由您的代码还是其他任何人启动的 .
此解决方案适用于iOS 8.X和iOS 9.X,并且缺少色彩更改后呈现方法的闪烁 . 对于UIAlertController的子视图的视图层次结构,它也是完全不可知的 .
快乐的黑客!
我刚刚完成了
UIAlertController
的替换 . This是唯一合理的方式,我认为:老
这是我在Swift中的方法,它从这里的答案中汲取了大量信息
这在某些情况下非常有效,而在其他情况下则完全失败(色调颜色未按预期显示) .
解决方案/ Hack for iOS9
您可以使用像PMAlertController这样的外部库而无需使用解决方法,您可以使用超级自定义警报替换Apple的不可自定义的UIAlertController .
Compatible with Xcode 8, Swift 3 and Objective-C
特点:
[x] Headers 视图
[x] Headers 图片(可选)
[x] Headers
[x]说明消息
[x]自定义:字体,颜色,尺寸等
[x] 1,2个按钮(水平)或3个按钮(垂直)
[x]按下按钮时关闭
[x]文本字段支持
[x]与UIAlertController类似的实现
[x] Cocoapods
[x]迦太基
[x]使用UIKit Dynamics进行动画制作
[x] Objective-C兼容性
[x] Swift 2.3和Swift 3支持
我在Urban Outfitters工作 . 我们有一个开源pod,
URBNAlert
,我们在所有应用程序中都使用了它 . 它基于UIAlertController
,但可高度自定义 .来源在这里:https://github.com/urbn/URBNAlert
或者只需在您的Podfile中放置
URBNAlert
即可通过pod进行安装下面是一些示例代码:
请找this类别 . 我能够改变UIAlertAction和UIAlertController的FONT和Color .
使用:
这是Swift 4.1和Xcode 9.4.1的扩展:
有点笨重,但现在我可以设置背景和文字颜色 . 我发现它here .
要将一个按钮(如CANCEL)的颜色更改为红色,您可以使用名为UIAlertActionStyle.destructive的样式属性:
我创建了一个方法Objective-C
代码在Xcode 8.3.1上完美地运行 . 您可以根据需要定制 .
我只是使用这种需求,看似和系统,细节略有不同,所以我们...... OC实现了Alert和Sheet弹出窗口封装 .
在日常开发中经常遇到需要添加一个数字来提醒或更改按钮颜色,如“简单”需求,今天带来的和系统组件高度相似,可以完全满足定制包装组件的需求 .
Github:https://github.com/ReverseScale/RSCustomAlertView
对于iOS 9.0及更高版本,请在app delegate中使用此代码
在Swift 4.1和Xcode 9.4.1中