Mad*_*des 1 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"];
Run Code Online (Sandbox Code Playgroud)
我不确定我是否100%正确地设置了alertViewPadding来使它居中,但我认为它是可行的。