我正在尝试创建具有样式UIAlertControllerStyleActionSheet(以前称为UIActionSheet)的自定义UIAlertController,并且遇到了很多麻烦,无法简单地对其进行自定义。我希望UIAlertAction 在左侧包含一个图像并减小按钮的大小。
我知道这是可行的:
(这正是我想要的)
我搜索了几个小时,但在互联网上找不到任何可以帮助我完成此简单任务的tut。另外,由于UIActionSheet自IOS 9起就已弃用,因此每当我尝试查找解决方案时,它便不再可用。另外,我读到UIAlertController不打算被子类化。
有谁知道如何实现警报的轻量级定制?我是否必须对UIView进行细分才能制作自己的警报表?
此解决方案确实使用私有API /属性。根据我的研究和经验,这是我知道可以自定义UIAlertController的唯一方法。如果查看公共头,则UIAlertContoller几乎没有自定义空间。但是,在iOS 8中启动UIAlertController之后,此解决方案在开发人员中通常会使用。您完全可以依赖Github的依赖功能。希望我的回答能解决您的问题。我相信这是您要寻找的,结果看起来像这样:
首先,您必须创建一个 UIAlertController
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
Run Code Online (Sandbox Code Playgroud)
自定义字体!即使只是某些字符。
NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... StackOverFlow!"];
[hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30.0] range:NSMakeRange(24, 11)];
[alertVC setValue:hogan forKey:@"attributedTitle"];
Run Code Online (Sandbox Code Playgroud)
现在,让我们创建一个UIAlertAction,您可以在其中添加动作处理程序以及图标。
UIAlertAction *button = [UIAlertAction actionWithTitle:@"First Button"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){
//add code to make something happen once tapped
}];
UIAlertAction *button2 = [UIAlertAction actionWithTitle:@"Second Button"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){
//add code to make something happen once tapped
}];
Run Code Online (Sandbox Code Playgroud)
在这里,您将图标添加到AlertAction。对我来说,您必须指定UIImageRenderingModeAlwaysOriginal
[button setValue:[[UIImage imageNamed:@"image.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[alertVC addAction:button2];
[alertVC addAction:button];
Run Code Online (Sandbox Code Playgroud)
记住要展示ViewController
[self presentViewController:alertVC animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)