gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:35:34 24 4
gpt4 key购买 nike

我有这段代码,它使用 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 中,我尝试过的都没有用。

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

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)

}
}

最佳答案

试试这个;

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
}

关于swift - 如何快速将 UIImageView 添加到导航栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56689644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com