如何将 UIView 添加到 navigationItem.backBarButtonItem
How to add a UIView into navigationItem.backBarButtonItem
我正在努力实现这个
这是我的代码
let contactName=UILabel()
contactName.text=chat?.Name
contactName.font=UIFont(name: "System", size: 17)
contactName.sizeToFit()
let contactImg=UIImageView()
contactImg.image=UIImage(named: (chat?.Image)!)
contactImg.frame.size.width=20
contactImg.frame.size.height=20
contactImg.layer.cornerRadius=contactImg.frame.height/2
let backButtonView=UIView()
backButtonView.addSubview(contactImg)
backButtonView.addSubview(contactName)
backButtonView.frame.size.width=20+contactName.frame.width
backButtonView.frame.size.height=max(contactImg.frame.height,contactName.frame.height)
navigationItem.backBarButtonItem=UIBarButtonItem(customView: backButtonView)
但是当我 运行 应用程序时它显示默认的后退栏按钮
我哪里做错了,请指导。
let profileImage = UIImage(named: "profile_image")
self.navigationController?.navigationBar.backIndicatorImage = profileImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = profileImage
self.navigationController?.navigationBar.backItem?.title = "Jhon"
Aditya 是对的,但这里有更多的解释。来自 Apple 文档:
When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.
https://developer.apple.com/documentation/uikit/uinavigationitem/1624958-backbarbuttonitem
此外,UIBarButtonItem“...专门用于放置在工具栏或选项卡栏上。您通常使用 Interface Builder 来创建和配置栏按钮项。”
https://developer.apple.com/documentation/uikit/uibarbuttonitem
我正在努力实现这个
这是我的代码
let contactName=UILabel()
contactName.text=chat?.Name
contactName.font=UIFont(name: "System", size: 17)
contactName.sizeToFit()
let contactImg=UIImageView()
contactImg.image=UIImage(named: (chat?.Image)!)
contactImg.frame.size.width=20
contactImg.frame.size.height=20
contactImg.layer.cornerRadius=contactImg.frame.height/2
let backButtonView=UIView()
backButtonView.addSubview(contactImg)
backButtonView.addSubview(contactName)
backButtonView.frame.size.width=20+contactName.frame.width
backButtonView.frame.size.height=max(contactImg.frame.height,contactName.frame.height)
navigationItem.backBarButtonItem=UIBarButtonItem(customView: backButtonView)
但是当我 运行 应用程序时它显示默认的后退栏按钮
我哪里做错了,请指导。
let profileImage = UIImage(named: "profile_image")
self.navigationController?.navigationBar.backIndicatorImage = profileImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = profileImage
self.navigationController?.navigationBar.backItem?.title = "Jhon"
Aditya 是对的,但这里有更多的解释。来自 Apple 文档:
When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.
https://developer.apple.com/documentation/uikit/uinavigationitem/1624958-backbarbuttonitem
此外,UIBarButtonItem“...专门用于放置在工具栏或选项卡栏上。您通常使用 Interface Builder 来创建和配置栏按钮项。”
https://developer.apple.com/documentation/uikit/uibarbuttonitem