[英]Trying to make rightBarButton appear after making it disappear using Swift
[英]RightBarButton doesn't appear but it appears when the ViewController disappear for a second
在我的應用程序中,第一個 ViewController 出現在右側導航欄項目中。 我想在通過推送出現的子 VC 的右側顯示不同的欄項目。 第一個 VC 中的項目顯示正常,但第二個沒有。 當 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
他們做了幾乎相同的事情,但第二個不起作用。
這是我錄制的gif文件。 當第二個 VC 消失時,您可以看到Edit
按鈕一秒鍾。 我試圖找到線索,但我找不到。 請檢查並給我任何意見。 謝謝你。
在它出現的任何地方刪除短語navigationController?.navigationBar.topItem
,永遠不要再使用它。 它將完全破壞導航 controller - 正如您自己所證明的那樣。
視圖 controller 可以與之對話的唯一導航項是它自己的navigationItem
屬性。 因此,第一個 vc 比第二個 vc 正確得多,因此它比第二個 vc 工作得更好。
您不應該使用“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.