代码之家  ›  专栏  ›  技术社区  ›  Sujal

UIAlertController中的居中对齐图像操作

  •  1
  • Sujal  · 技术社区  · 7 年前

    我有一个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

    2 回复  |  直到 7 年前
        1
  •  9
  •   mugx    7 年前

    你不应该使用 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

        2
  •  1
  •   Akashdeep Singh    7 年前
            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)