我将BarButtonItem添加到导航栏。此导航控制器和栏不是来自故事板,而是以编程方式生成。我注意到的一个问题是任何BarButton项目的位置都在导航栏的中心。无论是RightBarButton还是LeftBarButton,按钮始终显示在条形图的中心。我怀疑按钮项的可用空间为零,导致按钮默认为中心位置。我在下面附上了我的代码。问题出在哪里?
以编程方式创建导航控制器:
let popoverContent = Itinerary()
popoverContent.parentController = self
let nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.popover
let popover = nav.popoverPresentationController
popover?.sourceView = self.view
nav.navigationBar.barTintColor = UIColor.init(red: 154/255, green: 203/255, blue: 231/255, alpha: 1)
self.present(nav, animated: true, completion: nil)
从行程视图中添加BarButtonItems:
let button = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 40, height: 40))
let img = UIImage.init(named: "xbutton")
button.setImage(img, for: UIControlState.normal)
button.imageView?.contentMode = .scaleAspectFit
button.addTarget(self, action: #selector(dismissItin), for: .touchUpInside)
let Barbutton = UIBarButtonItem.init(customView: button)
self.navigationItem.rightBarButtonItem = Barbutton