【问题标题】:Centering an image in UIAlertAction in UIAlertController在 UIAlertController 中的 UIAlertAction 中将图像居中
【发布时间】:2016-01-18 05:58:00
【问题描述】:

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

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

【问题讨论】:

标签: ios swift uialertcontroller uialertaction


【解决方案1】:

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

【讨论】:

  • 在我的情况下是不正确的。 =.=。内边距总是太大,图像总是在右边。
【解决方案2】:

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

【讨论】:

  • 可以在本机 alertController 中更改图像偏移量。检查@Andrew Johnson 的答案
猜你喜欢
  • 2014-12-08
  • 2018-06-12
  • 2022-10-06
  • 1970-01-01
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-25
  • 2018-05-26
相关资源
最近更新 更多