从库中更改UIBarButton图像


CTO啊哦
2025-03-15 11:36:40 (5天前)


我有UIBarButton,用户可以更改图像,只需用图像选择器选择照片或图库图像。我的问题是规模图像不好。

如果我使用AspectFit,我的UIBarButton看起来像这样:
如果我用…

4 条回复
  1. 0# Fire ming | 2019-08-31 10-32




    这是工作代码(swift 4)
    </强>




    1. override func viewDidLoad() {

    2. let containView = UIView(frame: CGRect(x: 0, y: 0, width: 35, height: 35))
    3. let Button : UIButton = UIButton.init(type: .custom)
    4. Button.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
    5. Button.Round = true
    6. Button.setImage(UIImage(named: "photoIcon"), for: .normal)
    7. Button.addTarget(self, action: #selector(btnTapped), for: .touchUpInside)   
    8. containView.addSubview(Button)
    9. let rightBarButton = UIBarButtonItem(customView: containView)
    10. self.navigationItem.rightBarButtonItem = rightBarButton
    11. }

    12. </code>



    按钮动作在这里
    </强>




    1. @objc func btnTapped(_ sender: Any) {
      print(“Click Event”)
      }

    2. </code>


    圆形扩展




    1. extension UIView{

    2. @IBInspectable var Round:Bool{
    3.     get{
    4.         return false
    5.     }
    6.     set{
    7.         if newValue == true {
    8.             self.layer.cornerRadius = self.frame.size.height/2;
    9.             self.layer.masksToBounds = true;
    10.         }
    11.     }
    12. }
    13. }

    14. </code>



    输出:
    </强>







  2. 1# 银角 | 2019-08-31 10-32



    据我了解你的问题。我找到了解决方案。




    试试这个
    </强>




    1. func createPhotoBarButton(image: Data) {
      let barbtn = UIBarButtonItem()

    2.     let imageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0))
    3.     var image = UIImage(data:image)
    4.     if image == nil {
    5.         image = UIImage(named: "photoIcon")?.resize(maxWidthHeight: Double(imageView.frame.size.width))
    6.     }
    7.     imageView.image = image
    8.     imageView.contentMode = .scaleAspectFill
    9.     imageView.layer.cornerRadius = imageView.frame.size.height / 2
    10.     imageView.layer.masksToBounds = true
    11.     imageView.clipsToBounds = true
    12.     self.navigationItem.rightBarButtonItem = barbtn
    13.     barbtn.customView = imageView
    14.     barbtn.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(photoTapped(_:))))
    15. }

    16. </code>



    调整大小的方法
    </强>




    1. extension UIImage {

    2. func resize(maxWidthHeight : Double)-> UIImage? {
    3.     let actualHeight = Double(size.height)
    4.     let actualWidth = Double(size.width)
    5.     var maxWidth = 0.0
    6.     var maxHeight = 0.0
    7.     if actualWidth > actualHeight {
    8.         maxWidth = maxWidthHeight
    9.         let per = (100.0 * maxWidthHeight / actualWidth)
    10.         maxHeight = (actualHeight * per) / 100.0
    11.     }else{
    12.         maxHeight = maxWidthHeight
    13.         let per = (100.0 * maxWidthHeight / actualHeight)
    14.         maxWidth = (actualWidth * per) / 100.0
    15.     }
    16.     let hasAlpha = true
    17.     let scale: CGFloat = 0.0
    18.     UIGraphicsBeginImageContextWithOptions(CGSize(width: maxWidth, height: maxHeight), !hasAlpha, scale)
    19.     self.draw(in: CGRect(origin: .zero, size: CGSize(width: maxWidth, height: maxHeight)))
    20.     let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    21.     return scaledImage
    22. }
    23. }

    24. </code>



    产量
    </强>







  3. 2# 谦逊的毛巾 | 2019-08-31 10-32



    尝试使用以下代码创建自定义按钮:




    1. func createPhotoBarButton(image: Data) {
      let imageBtnContainer = UIButton()
      let imageBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
      var image = UIImage(data:image)
      if image == nil {
      image = UIImage(named: photoIcon”)
      }
      imageBtn.setImage(image, forState: UIControlState.Normal)
      imageBtn.frame = CGRectMake(0, 0, 53, 31)
      imageBtn.imageView?.contentMode = .scaleAspectFit

    2. imageBtnContainer.addSubview(imageBtn)
    3. self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: imageBtnContainer)
    4. ...
    5. }

    6. </code>


    你不再需要了

    resizedImage(..)

    功能


登录 后才能参与评论