[英]Unable to add image (profile picture) button to UINavigationBar
I'm trying to get my UINavigationBar
to display a circular profile picture in the upper left. 我试图让我的
UINavigationBar
在左上角显示一个圆形的个人资料图片。 I can make it display images/icons with ease using the following instantiation. 我可以使用以下实例轻松显示图像/图标。
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: icon, style: .plain, target: self, action: #selector(funcToCall))
The issue is that when trying to work with profile pictures, neither custom views, not image instantiation produce the desired result. 问题是,当尝试使用个人资料图片时,自定义视图和图像实例化都不会产生期望的结果。
Attempt 1 -- Swift 4 (Image Instantiation) 尝试1-快速4(图像实例化)
let img = originalImage.withRenderingMode(.alwaysOriginal)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: img, style: .plain, target: self, action: #selector(goToProfile))
This attempt produces the following result . 尝试产生以下结果 。
Problems: 问题:
leftBarButtonItem
, it is showing up in the middle leftBarButtonItem
,它也显示在中间 cornerRadius
for. cornerRadius
。 Attempt 2 -- Swift 4 (Custom Views) 尝试2-快速4(自定义视图)
let idealButton = UIButton(frame: CGRect(x: 0, y: 0, width: 32, height: 32))
idealButton.setImage(imgToUse, for: .normal)
idealButton.imageView?.contentMode = .scaleAspectFill
idealButton.imageView?.layer.cornerRadius = 0.5 * idealButton.frame.width
idealButton.imageView?.layer.borderWidth = 0.75
idealButton.imageView?.layer.borderColor = rgba(240,240,240,1).cgColor
idealButton.addTarget(self, action: #selector(goToProfile), for: .touchUpInside)
navigationItem.setLeftBarButton(UIBarButtonItem(customView: idealButton), animated: false)
This attempt produces the following result . 尝试产生以下结果 。
Problems 问题
UINavigationBar
. UINavigationBar
。 It is not constrained to the dimensions specified. Any help is much appreciated. 任何帮助深表感谢。 Thanks!
谢谢!
Let's try it with UIButton
added as subview of UINavigationBar
让我们尝试将
UIButton
添加为UINavigationBar
子视图
So start with creating button and setting its properties (you can use lazy
variable in global scope) 因此,从创建按钮并设置其属性开始(您可以在全局范围内使用
lazy
变量)
lazy var idealButton: UIButton = {
let button = UIButton()
button.setImage(imgToUse, for: .normal)
button.imageView?.layer.cornerRadius = 16
... // set other button's properties (border, target, colors, etc.)
return button
}()
now add this button as subview of current navigationController
's navigationBar
现在将此按钮添加为当前
navigationController
的navigationBar
子视图
guard let navigationBar = navigationController?.navigationBar else { return }
navigationBar.addSubview(idealButton)
then just set constraints for your button (you need to set height and width as you need and left and bottom constraints equal to navigationBar
's constraints) 然后只需为按钮设置约束即可(您需要根据需要设置高度和宽度,左约束和底约束等于
navigationBar
的约束)
idealButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
idealButton.leftAnchor.constraint(equalTo: navigationBar.leftAnchor, constant: 20),
idealButton.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor, constant: -6),
idealButton.heightAnchor.constraint(equalToConstant: 32),
idealButton.widthAnchor.constraint(equalToConstant: 32)
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.