我在UIViewControllers中有一个函数来设置UINavigationBar,它在许多函数中重复。我想在扩展中创建导航栏,但我想在每个函数中设置标题文本和购物车标签。我怎样才能做到这一点?我认为答案是使用协议,但我不知道如何。
这是我的扩展
extension UIViewController {
func shoppingBagButtonTouched(button: UIButton) {
-----
}
func closeView() {
dismiss(animated: true, completion: nil)
}
func setupNavigationHeader(showCart: Bool? = true) {
let navigationBar: UINavigationBar = {
let navBar = UINavigationBar(frame: CGRect(0, 0, self.view.frame.size.width, Constants.HEADER_HEIGHT))
return navBar
}()
let navigationItem = UINavigationItem()
self.automaticallyAdjustsScrollViewInsets = false
UINavigationBar.appearance().barTintColor = .red
UINavigationBar.appearance().tintColor = .white
let titleLabel: UILabel = {
let label = UILabel()
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = .clear
label.layer.masksToBounds = true
label.minimumScaleFactor = 10/UIFont.labelFontSize
label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 1
label.text = "not set"
label.textColor = .white
return label
}()
let fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
let menuBtn = UIBarButtonItem(image: UIImage(named: "closeNav"), style: .plain, target: self, action: #selector(self.closeView))
navigationItem.leftBarButtonItems = [fixedSpace, menuBtn]
navigationBar.items = [navigationItem]
if let showCart = showCart {
let cartCountLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: -0, width: 20, height: 20))
label.textAlignment = .center
label.layer.cornerRadius = label.bounds.size.height / 2
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = .clear
label.layer.masksToBounds = true
label.textColor = .white
label.minimumScaleFactor = 10/UIFont.labelFontSize
label.adjustsFontSizeToFitWidth = true
return label
}()
let shoppingBagButton: UIButton = {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 22, height: 22))
button.setBackgroundImage(UIImage(named: "shopping_bag"), for: .normal)
return button
}()
let rightBarButtonItem = UIBarButtonItem(customView: shoppingBagButton)
navigationItem.setRightBarButtonItems([rightBarButtonItem], animated: true)
shoppingBagButton.addTarget(self, action: #selector(shoppingBagButtonTouched(button:)), for: .touchUpInside)
shoppingBagButton.addSubview(cartCountLabel)
cartCountLabel.anchorCenterXToSuperview()
cartCountLabel.anchorCenterYToSuperview(constant: 2)
}
navigationBar.addSubview(titleLabel)
view.addSubview(navigationBar)
titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
titleLabel.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor, constant: -10).isActive = true
titleLabel.heightAnchor.constraint(equalToConstant: 20.0).isActive = true
titleLabel.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - 90).isActive = true
}
}
在每个控制器中,我都有这样的代码来设置标题标签和购物车标签,但是当我在扩展程序中创建导航栏时它不起作用。
var titleLabelText: String = "title not set"
var cartCount: String? {
didSet {
cartCountLabel.text = cartCount
}
}
func getCartCount() {
ServerUtility.getCartCountApi { (cartCount) in
if let count = cartCount as Int? {
if count > 0 {
self.cartCount = "\(count)"
}
} else {
self.cartCount = "0"
}
}
}
答案 0 :(得分:1)
你可能最好创建一个扩展UIViewController
这样的基类:
class BaseViewController: UIViewController {
let cartCountLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: -0, width: 20, height: 20))
label.textAlignment = .center
label.layer.cornerRadius = label.bounds.size.height / 2
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = .clear
label.layer.masksToBounds = true
label.textColor = .white
label.minimumScaleFactor = 10/UIFont.labelFontSize
label.adjustsFontSizeToFitWidth = true
return label
}()
func shoppingBagButtonTouched(button: UIButton) {
-----
}
func closeView() {
dismiss(animated: true, completion: nil)
}
func setupNavigationHeader(showCart: Bool? = true) {
let navigationBar: UINavigationBar = {
let navBar = UINavigationBar(frame: CGRect(0, 0, self.view.frame.size.width, Constants.HEADER_HEIGHT))
return navBar
}()
let navigationItem = UINavigationItem()
self.automaticallyAdjustsScrollViewInsets = false
UINavigationBar.appearance().barTintColor = .red
UINavigationBar.appearance().tintColor = .white
let titleLabel: UILabel = {
let label = UILabel()
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = .clear
label.layer.masksToBounds = true
label.minimumScaleFactor = 10/UIFont.labelFontSize
label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 1
label.text = "not set"
label.textColor = .white
return label
}()
let fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
let menuBtn = UIBarButtonItem(image: UIImage(named: "closeNav"), style: .plain, target: self, action: #selector(self.closeView))
navigationItem.leftBarButtonItems = [fixedSpace, menuBtn]
navigationBar.items = [navigationItem]
if let showCart = showCart {
let shoppingBagButton: UIButton = {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 22, height: 22))
button.setBackgroundImage(UIImage(named: "shopping_bag"), for: .normal)
return button
}()
let rightBarButtonItem = UIBarButtonItem(customView: shoppingBagButton)
navigationItem.setRightBarButtonItems([rightBarButtonItem], animated: true)
shoppingBagButton.addTarget(self, action: #selector(shoppingBagButtonTouched(button:)), for: .touchUpInside)
shoppingBagButton.addSubview(cartCountLabel)
cartCountLabel.anchorCenterXToSuperview()
cartCountLabel.anchorCenterYToSuperview(constant: 2)
}
navigationBar.addSubview(titleLabel)
view.addSubview(navigationBar)
titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
titleLabel.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor, constant: -10).isActive = true
titleLabel.heightAnchor.constraint(equalToConstant: 20.0).isActive = true
titleLabel.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - 90).isActive = true
}
}
然后,您可以从基类派生所有视图控制器实例,如下所示:
class MyViewController:BaseViewController {
var titleLabelText: String = "title not set"
var cartCount: String? {
didSet {
cartCountLabel.text = cartCount
}
}
func getCartCount() {
ServerUtility.getCartCountApi { (cartCount) in
if let count = cartCount as Int? {
if count > 0 {
self.cartCount = "\(count)"
}
} else {
self.cartCount = "0"
}
}
}
}
我没有通过运行验证上面的代码,因此可能需要稍微调整一下:)