问题描述
我正在尝试创建一种样式为 UIAlertControllerStyleActionSheet (以前称为UIActionSheet)的自定义 UIAlertController ,而在简单地对其进行自定义方面遇到了很多麻烦.我希望UIAlertAction 在左侧包含一个图像,并减小按钮的大小.
I'm trying to create a custom UIAlertController with style UIAlertControllerStyleActionSheet (formerly UIActionSheet) and I'm encountering so much trouble to simply customize it. I want my UIAlertAction to contain an image on the left and decrease buttons size.
我知道这是可行的:
(这正是我想要的)
我搜索了几个小时,但在互联网上找不到任何可以帮助我完成此简单任务的tut.另外,由于UIActionSheet自IOS 9起就已弃用,所以每当我尝试找到一种解决方案时,它便不再可用.另外,我读到UIAlertController不打算被子类化.
I searched for hours but I can't find any tut's on the internet that can help me to achieve that simple task. Plus, as UIActionSheet is deprecated since IOS 9, anytime I try to find a solution it's not usable anymore. Also, i read that UIAlertController is not intended to be subclassed...
有人知道如何实现对我的警报的轻量级定制吗?我是否必须对UIView进行细分才能制作自己的警报表?
Does anyone know how to achieve that light customization of my alert? Do I have to sublass a UIView to make my own alertsheet?
推荐答案
此解决方案确实使用私有API/属性.根据我的研究和经验,这是我知道可以自定义UIAlertController的唯一方法.如果查看公共头,则UIAlertContoller几乎没有自定义空间.但是,在iOS 8中启动UIAlertController之后,此解决方案在开发人员中通常会使用.您完全可以依赖Github的依赖功能.希望我的回答能解决您的问题.我相信这就是您要寻找的,结果看起来像这样:
This solution does use private APIs/properties. Based my research and experience, this is the only way I know that you can customize UIAlertController. If you look at the public header, UIAlertContoller has little room for customization. However, this solution is commonly used among developers after the launch of UIAlertController in iOS 8. You can totally rely on depedencies from Github. I hope my answer can solve your problem. I believe this is what you are looking for, the result loooks like this:
首先,您必须创建一个UIAlertController
First, you have to create a UIAlertController
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
自定义字体!即使是某些角色也是如此.
Custom font! even for just certain characters.
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"];
现在,让我们创建一个UIAlertAction,您可以在其中添加动作处理程序以及图标.
Now, let's create a UIAlertAction, where you can add action handlers and also add icons.
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
}];
在这里,您将图标添加到AlertAction.对我来说,您必须指定UIImageRenderingModeAlwaysOriginal
Here, you add the icon to the AlertAction. For me, you have to specify UIImageRenderingModeAlwaysOriginal
[button setValue:[[UIImage imageNamed:@"image.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[alertVC addAction:button2];
[alertVC addAction:button];
记住要展示ViewController
Remember to present the ViewController
[self presentViewController:alertVC animated:YES completion:nil];
这篇关于UIAlertController和UIAlertControllerStyleActionSheet定制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!