【问题标题】:How to add UIImageView to navigation bar in swift?如何快速将 UIImageView 添加到导航栏?
【发布时间】:2019-11-03 11:46:32
【问题描述】:

我有这段代码使用 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 添加到导航栏,我认为不需要点击手势,因此您可以忽略它。我发现了一些类似的问题,但它们在目标 C 中,我尝试过的都没有。

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

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)

    }
}

【问题讨论】:

标签: swift uinavigationcontroller uiimageview


【解决方案1】:

试试这个;

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
  }

【讨论】:

    【解决方案2】:
        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) {
    
        }
    

    【讨论】:

    猜你喜欢
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多