我正在构建一个在rightBarButtonItem中具有购物车的电子商务应用程序。我已实现扩展以在每个UIViewControllers中添加购物车barButtonItem。我的barButtonItem在模拟器设备(包括iPhone 6s,iPhone SE和iPhoneX模拟器)中正确显示。没有在我的物理设备iPhone 6s上显示。有人可以帮助我如何跟踪此问题?感谢您的关注
这是我的代码
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
}
}
最佳答案
尝试这个,
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;
}
该代码对我有用