【发布时间】:2021-06-03 13:43:58
【问题描述】:
【问题讨论】:
标签: swift uinavigationbar
【问题讨论】:
标签: swift uinavigationbar
在 viewDidLoad 中设置您的自定义按钮导航栏:
let button = UIButton(type: .custom)
//Set the image
button.setImage(UIImage(systemName: "chevron.backward"), for: .normal)
//Set the title
button.setTitle("Yourtitle", for: .normal)
//Add target
button.addTarget(self, action: #selector(callMethod), for: .touchUpInside) //call button tap action
// Add insets padding
let spacing: CGFloat = -10 // the amount of spacing to appear between image and title
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: spacing, bottom: 0, right: 0)
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) // customize your button titleEdgeInsets if you want
//Create bar button
let barButton = UIBarButtonItem(customView: button)
navigationItem.leftBarButtonItem = barButton
然后创建你的按钮点击功能:
@objc fileprivate func callMethod() {
print("Your code here")
}
【讨论】:
UIBarButtonItem(customView:) 最后 (3) 使用 navigationItem.leftBarButtonItem 使其成为导航栏的左栏按钮。