rightBarButtonItem未在物理设备上显示

时间:2018-02-13 14:34:16

标签: ios swift uikit uibarbuttonitem

我正在构建一个在rightBarButtonItem中有购物车的电子商务应用程序。我已经实现了在每个UIViewControllers中添加购物车barButtonItem的扩展。我的barButtonItem在模拟器设备(包括iPhone 6s,iPhone SE和iPhoneX模拟器)中正常显示但它没有显示在我的物理设备上,iPhone 6s.Could任何人都可以帮我跟踪这个问题?感谢您的关注 这是我的代码

extension UIViewController{
    func addCartBarButtonItem(){
        let cartButton = UIButton(type: .custom)
        cartButton.setImage(UIImage(named:"cart"), for: .normal)
        cartButton.widthAnchor.constraint(equalToConstant: 28.0).isActive = true
        cartButton.heightAnchor.constraint(equalToConstant: 28.0).isActive = true
        cartButton.addTarget(self, action: #selector(self.goCheckOutController), for: .touchUpInside)
        let cartBarButtonItem = UIBarButtonItem(customView: cartButton)
        cartBarButtonItem.addBadge(number: CartDataManager.loadItems().count)
        self.navigationItem.rightBarButtonItem = cartBarButtonItem
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个,

var rightSearchBarButtonItem : UIBarButtonItem?

func addCartButtonToNavigationBar(){

    self.rightSearchBarButtonItem =  UIBarButtonItem(image: #imageLiteral(resourceName: "cart") , style: UIBarButtonItemStyle.plain, target: self, action: #selector(goCheckOutController))
    self.navigationItem.rightBarButtonItem = self.rightSearchBarButtonItem;
}

此代码对我有用