[英]RightBarButton doesn't appear but it appears when the ViewController disappear for a second
In my app, the first ViewController appears with right navigation bar items.在我的应用程序中,第一个 ViewController 出现在右侧导航栏项目中。 I want to show different bar items on right side in child VC which is appeared by pushing.
我想在通过推送出现的子 VC 的右侧显示不同的栏项目。 The items in the first VC shows fine, but the second doesn't.
第一个 VC 中的项目显示正常,但第二个没有。 The bar button shows for a second when the VC disappeared.
当 VC 消失时,条形按钮会显示一秒钟。
// The things the first VC did
navigationItem.setHidesBackButton(true, animated: true)
navigationController?.navigationBar.tintColor = .gray600
let stackView = UIStackView()
stackView.addArrangedSubviews(views: [registerButton, notificationButton])
stackView.spacing = 16
let rightBarButton = UIBarButtonItem(customView: stackView)
navigationController?.navigationBar.topItem?.rightBarButtonItem = rightBarButton
// The things the second did
navigationController?.navigationBar.tintColor = .gray600
navigationController?.navigationBar.topItem?.backButtonTitle = ""
navigationController?.navigationBar.backIndicatorImage = .back
navigationController?.navigationBar.backIndicatorTransitionMaskImage = .back
let rightBarButton = UIBarButtonItem(customView: editButton)
navigationController?.navigationBar.topItem?.rightBarButtonItem = rightBarButton
They did almost same things but the second doesn't work.他们做了几乎相同的事情,但第二个不起作用。
Here is the gif file i recorded.这是我录制的gif文件。 You can see
Edit
button for a second when the second VC disappeared.当第二个 VC 消失时,您可以看到
Edit
按钮一秒钟。 I tried to find the clues but i couldn't.我试图找到线索,但我找不到。 Please check it and give me any comments.
请检查并给我任何意见。 Thank you.
谢谢你。
Delete the phrase navigationController?.navigationBar.topItem
everywhere it appears, and never use it again, ever.在它出现的任何地方删除短语
navigationController?.navigationBar.topItem
,永远不要再使用它。 It will totally break the navigation controller — as you yourself have demonstrated.它将完全破坏导航 controller - 正如您自己所证明的那样。
The only navigation item a view controller can talk to is its own navigationItem
property.视图 controller 可以与之对话的唯一导航项是它自己的
navigationItem
属性。 Thus the first vc is much more correct than the second vc, and so it works much better than the second vc.因此,第一个 vc 比第二个 vc 正确得多,因此它比第二个 vc 工作得更好。
you should not set a button and its title by using "topItem", instead you should set them by using navigationItem's rightBarButtonItem.您不应该使用“topItem”来设置按钮及其标题,而应该使用navigationItem 的rightBarButtonItem 来设置它们。
let rightBarButton = UIBarButtonItem(customView: editButton)
navigationItem.rightBarButtonItem = rightBarButton
let backButton = UIBarButtonItem(...)
navigationItem.leftBarButtonItem = backButton
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.