HiD*_*ing 1 uinavigationcontroller uinavigationitem ios swift
所以我试图通过像这样子类化它来创建一个UIBarButtonItem自定义UIView。
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")
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我创建它并将其分配给我时,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.
}
}
Run Code Online (Sandbox Code Playgroud)
我看过其他帖子,似乎没有一个完全符合我的情况。我开始认为这是因为我没有给出UIView框架,但如果是这种情况,我不确定在这种情况下我将如何做到这一点。任何人看到任何我看不到的东西都可能帮助我解决这个问题。设置目标也不起作用我尝试了两种不同的方法,但都没有触发任何事情
@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")
}
Run Code Online (Sandbox Code Playgroud)
更改您的添加行
self.navigationController?.navigationItem.leftBarButtonItem = customLeftBar
Run Code Online (Sandbox Code Playgroud)
到
self.navigationItem.leftBarButtonItem = customLeftBar
Run Code Online (Sandbox Code Playgroud)
添加 barItem 时,您需要通过navigationItem,而ViewController不是NavigationController
编辑添加动作
您的自定义UIBarButtonItem是Custom View的BarButtonItem,所以target并selector不会工作。
您可以通过在 customView 中添加按钮来添加自定义操作,并通过闭包发送操作
初始化你的闭包
var didSelectItem: (() -> Void)?
Run Code Online (Sandbox Code Playgroud)
将创建按钮代码添加到您的 @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)
Run Code Online (Sandbox Code Playgroud)
并添加功能
@objc func didTap(_ button: UIButton) {
print("Did tap button")
}
Run Code Online (Sandbox Code Playgroud)
在您的 viewController 中,您可以通过
customLeftBar.didSelectItem = { [weak self] in
self?.leftBarPressed()
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,您的 barbuttonitem 的默认框架是 30x30,因此您必须为您的 barbuttonitem 设置框架。如果没有,您只能在 30x30 区域捕捉点击动作(取消注释代码以查看它)
| 归档时间: |
|
| 查看次数: |
957 次 |
| 最近记录: |