UIBarButtonItem 不显示
UIBarButtonItem not Showing
所以我试图通过像这样子类化它来创建一个带有自定义 UIView
的 UIBarButtonItem
。
import UIKit
import SnapKit
class LocationManager: UIBarButtonItem {
let createdView = UIView()
lazy var currentCityLabel: UILabel = {
let currentCityLabel = UILabel()
currentCityLabel.text = "Philadelphia, PA"
guard let customFont = UIFont(name: "NoirPro-SemiBold", size: 20) else {
fatalError("""
Failed to load the "CustomFont-Light" font.
Make sure the font file is included in the project and the font name is spelled correctly.
"""
)
}
currentCityLabel.adjustsFontForContentSizeCategory = true
return currentCityLabel
}()
lazy var downArrow: UIImageView = {
let downArrow = UIImageView()
downArrow.contentMode = .scaleAspectFit
downArrow.image = UIImage(named: "downArrow")
return downArrow
}()
override init() {
super.init()
setupViews()
}
@objc func setupViews(){
customView = createdView
createdView.addSubview(currentCityLabel)
currentCityLabel.snp.makeConstraints { (make) in
make.left.equalTo(createdView.snp.left)
make.top.bottom.equalTo(createdView)
}
createdView.addSubview(downArrow)
downArrow.snp.makeConstraints { (make) in
make.left.equalTo(currentCityLabel.snp.right).offset(5)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
但是,当我创建它并在我的 viewController
中分配它时,我什么也没看到
import UIKit
class ViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
@objc func setupViews(){
guard let collection = collectionView else {
return
}
collection.backgroundColor = .white
let customLeftBar = LocationManager()
self.navigationController?.navigationItem.leftBarButtonItem = customLeftBar
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我看过其他 post 和 none 似乎很符合我的情况。我开始认为这是因为我没有给 UIView
一个框架,但我不确定在这种情况下我会怎么做。任何人看到任何我没有看到的东西都可能帮助我解决这个问题。设置目标也不起作用我尝试了两种不同的方法,其中none触发了一个东西
@objc func setupBarButtonItems(){
let customLeftBar = LocationManager()
customLeftBar.action = #selector(self.leftBarPressed)
customLeftBar.target = self
customLeftBar.customView?.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.leftBarPressed))
customLeftBar.customView?.addGestureRecognizer(tapGesture)
navigationItem.leftBarButtonItem = customLeftBar
}
@objc func leftBarPressed(){
print("left bar tapped")
}
更改您的添加行
self.navigationController?.navigationItem.leftBarButtonItem = customLeftBar
到
self.navigationItem.leftBarButtonItem = customLeftBar
添加barItem时,需要通过ViewController
的navigationItem
添加,而不是NavigationController
已编辑以添加操作
您的自定义 UIBarButtonItem
是 Custom View
的 BarButtonItem
,因此 target
和 selector
将不起作用。
您可以通过在自定义视图中添加按钮来添加自定义操作,并通过闭包发送操作
初始化你的闭包
var didSelectItem: (() -> Void)?
在您的 @objc func setupViews()
中添加创建按钮代码
let button = UIButton(type: .custom)
createdView.addSubview(button)
button.snp.makeConstraints { (maker) in
maker.top.bottom.leading.trailing.equalTo(createdView)
}
// button.backgroundColor = UIColor.cyan // uncomment this line for understand about the barbuttonitem's frame
button.addTarget(self, action: #selector(didTap(_:)), for: .touchUpInside)
并添加函数
@objc func didTap(_ button: UIButton) {
print("Did tap button")
}
在您的 viewController 中,您可以通过
获得点击操作
customLeftBar.didSelectItem = { [weak self] in
self?.leftBarPressed()
}
不幸的是,您的 barbuttonitem 的默认框架是 30x30,因此您必须为您的 barbuttonitem 设置框架。如果没有,你只能在30x30区域捕捉到点击动作(取消注释代码才能看到)
所以我试图通过像这样子类化它来创建一个带有自定义 UIView
的 UIBarButtonItem
。
import UIKit
import SnapKit
class LocationManager: UIBarButtonItem {
let createdView = UIView()
lazy var currentCityLabel: UILabel = {
let currentCityLabel = UILabel()
currentCityLabel.text = "Philadelphia, PA"
guard let customFont = UIFont(name: "NoirPro-SemiBold", size: 20) else {
fatalError("""
Failed to load the "CustomFont-Light" font.
Make sure the font file is included in the project and the font name is spelled correctly.
"""
)
}
currentCityLabel.adjustsFontForContentSizeCategory = true
return currentCityLabel
}()
lazy var downArrow: UIImageView = {
let downArrow = UIImageView()
downArrow.contentMode = .scaleAspectFit
downArrow.image = UIImage(named: "downArrow")
return downArrow
}()
override init() {
super.init()
setupViews()
}
@objc func setupViews(){
customView = createdView
createdView.addSubview(currentCityLabel)
currentCityLabel.snp.makeConstraints { (make) in
make.left.equalTo(createdView.snp.left)
make.top.bottom.equalTo(createdView)
}
createdView.addSubview(downArrow)
downArrow.snp.makeConstraints { (make) in
make.left.equalTo(currentCityLabel.snp.right).offset(5)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
但是,当我创建它并在我的 viewController
中分配它时,我什么也没看到
import UIKit
class ViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
@objc func setupViews(){
guard let collection = collectionView else {
return
}
collection.backgroundColor = .white
let customLeftBar = LocationManager()
self.navigationController?.navigationItem.leftBarButtonItem = customLeftBar
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我看过其他 post 和 none 似乎很符合我的情况。我开始认为这是因为我没有给 UIView
一个框架,但我不确定在这种情况下我会怎么做。任何人看到任何我没有看到的东西都可能帮助我解决这个问题。设置目标也不起作用我尝试了两种不同的方法,其中none触发了一个东西
@objc func setupBarButtonItems(){
let customLeftBar = LocationManager()
customLeftBar.action = #selector(self.leftBarPressed)
customLeftBar.target = self
customLeftBar.customView?.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.leftBarPressed))
customLeftBar.customView?.addGestureRecognizer(tapGesture)
navigationItem.leftBarButtonItem = customLeftBar
}
@objc func leftBarPressed(){
print("left bar tapped")
}
更改您的添加行
self.navigationController?.navigationItem.leftBarButtonItem = customLeftBar
到
self.navigationItem.leftBarButtonItem = customLeftBar
添加barItem时,需要通过ViewController
的navigationItem
添加,而不是NavigationController
已编辑以添加操作
您的自定义 UIBarButtonItem
是 Custom View
的 BarButtonItem
,因此 target
和 selector
将不起作用。
您可以通过在自定义视图中添加按钮来添加自定义操作,并通过闭包发送操作
初始化你的闭包
var didSelectItem: (() -> Void)?
在您的 @objc func setupViews()
let button = UIButton(type: .custom)
createdView.addSubview(button)
button.snp.makeConstraints { (maker) in
maker.top.bottom.leading.trailing.equalTo(createdView)
}
// button.backgroundColor = UIColor.cyan // uncomment this line for understand about the barbuttonitem's frame
button.addTarget(self, action: #selector(didTap(_:)), for: .touchUpInside)
并添加函数
@objc func didTap(_ button: UIButton) {
print("Did tap button")
}
在您的 viewController 中,您可以通过
获得点击操作customLeftBar.didSelectItem = { [weak self] in
self?.leftBarPressed()
}
不幸的是,您的 barbuttonitem 的默认框架是 30x30,因此您必须为您的 barbuttonitem 设置框架。如果没有,你只能在30x30区域捕捉到点击动作(取消注释代码才能看到)