1

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

如果可能,我想将图像居中在 UIAlertAction 中。目前 UIAlertAction 的标题位于中心,图像显示在左侧。

4

2 回答 2

6

您实际上可以使用 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% 正确以使其完全居中,但我认为这是可行的。

在此处输入图像描述

于 2017-02-01T00:48:58.553 回答
-3

您不能更改图像的框架,您需要为您的问题构建自定义 alertView。

于 2016-01-18T06:04:32.077 回答