ios - 在 UIAlertController 的 UIAlertAction 中居中图像

标签 ios swift uialertcontroller uialertaction

我目前正在使用以下代码 (Swift) 将图像添加到 UIAlertAction。 purpleAction.setValue(pCircle, forKey: "image")

如果可能的话,我想将图像置于 UIAlertAction 的中心。目前 UIAlertAction 的标题位于中间,图像出现在左侧。

最佳答案

你实际上可以用 imageWithAlignmentRectInsets 破解它:

UIAlertController *ac = [UIAlertController alertControllerWithTitle:nil message:alertControllerWithTitle:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
UIImage *topoImage = [UIImage imageNamed:@"foo.png"];
CGFloat alertViewPadding = 22.0;
CGFloat left = -ac.view.frame.size.width/2 + topoImage.size.width + alertViewPadding;
UIImage *centeredTopoImage = [[topoImage imageWithAlignmentRectInsets:UIEdgeInsetsMake(0,left,0,0) ]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[action setValue:centeredTopoImage forKey:@"image"];

我不确定 alertViewPadding 软糖是否 100% 正确以使其完美居中,但我认为这是可行的。

enter image description here

关于ios - 在 UIAlertController 的 UIAlertAction 中居中图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34848127/

相关文章:

ios - 在靠近边框的文本框内设置颜色

ios - 从 com.apple.main-thread 入队(线程 1)崩溃 | iOS | swift 4.1

ios - 将当前 UIViewController 内置动画与其他动画同步

swift - UIAlertController : it does not close in a controlleView that a UICollectionView

ios - 在 Swift 中创建一个 UIAlertAction

ios - 如何在 watch 套件中显示通知,其中 iPhone 应用程序已经显示通知

ios - 保存在应用程序中拍摄的照片并使用核心数据

ios - Xcode 项目格式 : what are the differences between 3. 1、3.2、6.3 和 8.0?

swift - 点击时增加/减少表格 View 单元格的大小 - Swift

swift - 如何使警报只出现一次