swift 如何将 UIImageView 添加到导航栏?

How to add UIImageView to navigation bar in swift?

我有这段代码,它使用 UIImageView 在 UIImage 周围添加了一个圆角边框,我已经使用 UITapGestureRecognizer 让用户点击按钮:

var profilePicture = UIImageView()
func setupUserProfileButton() {

    let defaultPicture = UIImage(named: "profilePictureSmall")
    profilePicture = UIImageView(image: defaultPicture)
    profilePicture.layer.cornerRadius = profilePicture.frame.width / 2
    profilePicture.clipsToBounds = true
    profilePicture.layer.borderColor = UIColor.black.cgColor
    profilePicture.layer.borderWidth = 1

    // Letting users click on the image
    profilePicture.isUserInteractionEnabled = true
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(profilePictureTapped))
    profilePicture.addGestureRecognizer(tapGesture)

}

如何将它添加到导航栏的左侧?可能吗?如果我可以将 ImageView 作为 barButtonItem 添加到导航栏,我认为不需要点击手势,因此您可以忽略它。我有点发现了一些类似的问题,但它们在我尝试过的 objective C 和 none 中。

这是我根据一个答案得出的结论:

import UIKit
import Firebase

class CreateStoryPage: BaseAndExtensions {

    let userProfileButton = UIButton(type: .custom)

    override func viewDidLoad() {
        super.viewDidLoad()

        // Call all the elements
        setupUserProfileButton()

    }
    // MARK:- Setups
    // Setup the user profile button
    func setupUserProfileButton() {

        userProfileButton.setImage(#imageLiteral(resourceName: "profilePictureSmall.png"), for: .normal)
        userProfileButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        userProfileButton.addTarget(self, action: #selector(profilePictureTapped), for: .touchUpInside)

        let userProfileView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        userProfileView.layer.cornerRadius = 14
        userProfileView.backgroundColor = .red

        userProfileView.addSubview(userProfileButton)

        let leftNavBarItem = UIBarButtonItem(customView: userProfileView)
        self.navigationItem.setLeftBarButton(leftNavBarItem, animated: true)


    }

    // if user taps on profile picture
    @objc func profilePictureTapped() {

        let userProfilePage = UserProfilePage()
        present(userProfilePage, animated: true, completion: nil)

    }
}
    let navBtn = UIButton(type: .custom)
    navBtn.setImage("yourImage", for: .normal)
    navBtn.frame = CGRect(x: 0, y: 0, width: 28, height: 28)
    navBtn.addTarget(self, action: #selector(self.openProfile(_:)), for: .touchUpInside)

    let view = UIView(frame: CGRect(x: 0, y: 0, width: 28, height: 28))
    view.cornerRadius = 14
    view.backgroundColor = Global.colorBlue

    view.addSubview(navBtn)

    let leftNavBarItem = UIBarButtonItem(customView: view)
    self.navigationItem.setLeftBarButton(leftNavBarItem, animated: true)


    @objc
    func openProfile(_ sender: UIButton) {

    }

试试这个;

private func setupRightItem() {
    let userProfileButton = UIButton(type: .custom)
    userProfileButton.imageView?.contentMode = .scaleAspectFill
    userProfileButton.clipsToBounds = true
    userProfileButton.addTarget(self, action: #selector(profilePictureTapped), for: .touchUpInside)
    userProfileButton.setImage(#imageLiteral(resourceName: "profilePictureSmall.png"), for: .normal)
    userProfileButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)

    self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: userProfileButton)

    userProfileButton.widthAnchor.constraint(equalToConstant: 30).isActive = true
    userProfileButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
  }

  @objc private func goProfile() {
    /// -> Action
  }