在UIAlertController中居中对齐图像动作

时间:2018-01-02 16:37:49

标签: ios swift alignment uialertcontroller

我有一个UIAlertController:

let alert = UIAlertController(title: "Add your photo", message: "", preferredStyle: .alert)

var image = UIImage(named: "avatar.png")

image = image?.withAlignmentRectInsets(UIEdgeInsetsMake(0, view.frame.width/2 - (image?.size.width)!/2, 0, 0))

let imageAction = UIAlertAction(title: "", style: .default, handler: nil)

imageAction.setValue(image?.withRenderingMode(.alwaysOriginal), forKey: "image")

alert.addAction(imageAction)

默认情况下,图标左对齐,我尝试使用UIEdgeInset(上面的第3行)对齐它。考虑到左上角是原点(0,0),我试图将图像定位在水平轴的中心,左边的插入值与代码一样,但图像如下所示。所以我知道这是不正确的。我可以通过反复试验将其置于中心。但我想提出一种适当的方法来对齐它。实现这一目标的正确方法是什么?谢谢。

enter image description here

3 个答案:

答案 0 :(得分:5)

您不应该使用forKey: "image",因为它是private api

如果要在警报控制器中添加图像,可以尝试使用NSLayoutConstraint,这样做:

let alert = UIAlertController(title: "Add your photo", message: "\n\n\n", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Upload", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Add later", style: .default, handler: nil))
let image = UIImageView(image: UIImage(named: "avatar"))
alert.view.addSubview(image)
image.translatesAutoresizingMaskIntoConstraints = false
alert.view.addConstraint(NSLayoutConstraint(item: image, attribute: .centerX, relatedBy: .equal, toItem: alert.view, attribute: .centerX, multiplier: 1, constant: 0))
alert.view.addConstraint(NSLayoutConstraint(item: image, attribute: .centerY, relatedBy: .equal, toItem: alert.view, attribute: .centerY, multiplier: 1, constant: 0))
alert.view.addConstraint(NSLayoutConstraint(item: image, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 64.0))
alert.view.addConstraint(NSLayoutConstraint(item: image, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 64.0))
self.present(alert, animated: true, completion: nil)

结果是:

enter image description here

答案 1 :(得分:1)

THIS更新为swift,最终代码为:

    let alert = UIAlertController(title: "Add your photo", message: "", preferredStyle: .alert)

    let imageAction = UIAlertAction(title: "", style: .default, handler: nil)
    let image = UIImage(named: "IMG_0231")
    let alertViewPadding: CGFloat = 65.0 //Adjust this as per your need
    let left = -alert.view.frame.size.width / 2 + image!.size.width + alertViewPadding
    let centeredTopoImage = image?.withAlignmentRectInsets(UIEdgeInsetsMake(0, left, 0, 0)).withRenderingMode(.alwaysOriginal)
    imageAction.setValue(centeredTopoImage, forKey: "image")


    let uploadAction = UIAlertAction(title: "Upload", style: .default, handler: nil)

    let laterAction = UIAlertAction(title: "Add Later", style: .cancel, handler: nil)

    alert.addAction(imageAction)
    alert.addAction(uploadAction)
    alert.addAction(laterAction)
    self.present(alert, animated: true, completion: nil)

结果将是:

enter image description here

答案 2 :(得分:0)

        let alert = UIAlertController(title: "Unfollow John Doe?", message: "", preferredStyle: .actionSheet)

        let imageAction = UIAlertAction(title: "", style: .default, handler: nil)
        let image = UIImage(named: "user_img_a")
        let alertViewPadding: CGFloat = 65.0 //Adjust this as per your need
        let left = -alert.view.frame.size.width / 2 + image!.size.width/2 + 20
       // let left = alert.view.frame.size.width/2 - image!.size.width/2
        let centeredTopoImage = image?.withAlignmentRectInsets(UIEdgeInsetsMake(0, left, 0, 0)).withRenderingMode(.alwaysOriginal)
        imageAction.setValue(centeredTopoImage, forKey: "image")


        let  deleteButton = UIAlertAction(title: "Unfollow", style: .destructive, handler: { (action) -> Void in
            print("Delete button tapped")
            self.arrLikes[sender.tag]=0

            self.tblList.reloadData()
        })

        let cancelButton = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in
            print("Cancel button tapped")

        })

        alert.addAction(imageAction)
        alert.addAction(deleteButton)
        alert.addAction(cancelButton)
        self.present(alert, animated: true, completion: nil)