简体   繁体   English

RightBarButton 没有出现,但是当 ViewController 消失一秒钟时出现

[英]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.谢谢你。

6sbdoa

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.

相关问题 使用Swift使其消失后尝试显示rightBarButton - Trying to make rightBarButton appear after making it disappear using Swift 第二个ViewController返回时不显示信息 - Second ViewController doesn't show the information when coming back to it 在pushViewController之后没有出现ViewController - ViewController doesn't appear after pushViewController 导航控制器未出现在仪表板ViewController上 - Navigation Controller doesn't appear on Dashboard ViewController MvxTabBarViewController作为第一个ViewController没有出现 - MvxTabBarViewController as first ViewController doesn't appear 使用按钮切换ViewController不会在第二个ViewController中显示NavigationBar - Switching the ViewController with button doesn't shows the NavigationBar in second ViewController 标签栏n故事板上的rightbarbutton消失 - rightbarbutton disappear on tabbar n storyboard 搜索到新的ViewController后,搜索栏不会消失 - Search Bar doesn't disappear after segue to new ViewController 尽管调用了viewDidLoad,但未出现iOS ViewController - iOS ViewController doesn't appears although viewDidLoad gets called 打开应用程序时工具栏不会出现,但是如果我将设备横向放置 - Toolbar doesn't appear when open app but if i turn device to landscape it appears
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM