gpt4 book ai didi

swift - 存在后退按钮时无法设置 UINavigationBar 标题或将 UIButtons 放置在 UINavigationBar 中

转载 作者:行者123 更新时间:2023-11-28 13:40:30 27 4
gpt4 key购买 nike

我正在开发一个具有多个嵌套 UIViewController 的应用程序。我有两个 View ,一个是带有 uibarbuttonitem 的 Root View Controller ,连接到另一个 UIViewController。

被转至的 UIViewController 不接受 UIButtons 或 UIBarButtonItems,我似乎无法为其设置标题。

The Storyboard

我需要在 UINavigationBar 中放置一个标题和其他 UIElements,但似乎没有任何效果,而且我没有在网上找到任何解决方案。

UINavigationBar 甚至没有出现在 View 的层次结构中。

Hierarchy

感谢您的合作。

最佳答案

你是对的。对于 NavigationController 堆栈中的 ViewController,Storyboard 布局不一致且令人困惑。第一个提供对 NavigationItem 的直接访问,但后面的不提供。我想那是因为只有一个 NavigationItem,它需要随着 ViewController 的推送和弹出而动态更新。

处理这个问题的方法是在代码中完成。推送 ViewController 时,其 title 属性用于设置导航栏上的标题。因此,在 NavigationController 堆栈中的每个 ViewController 中,在 viewDidLoad 中设置其 title:

self.title = "titleForThisVC"

对于 rightBarButton,您也可以通过编程方式添加它。如果这是按钮的 @IBAction:

@objc func handleRightBarButton() {
print("right button")
}

然后您将像这样添加按钮(同样在 viewDidLoad 中):

self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Foo", style: .plain,
target: self, action: #selector(handleRightBarButton))

关于 UITabBarController 的最后一个问题

如果您的 NavigationControllers 是 UITabBarController 的子级,则选项卡栏中使用的标题将从代码设置的堆栈中的第一个 ViewController 中选取标题,但仅在第一次访问该选项卡之后。为了解决这个问题,Interface Builder

Attributes Inspector 中的顶部 ViewController 也应该设置 title 属性

关于swift - 存在后退按钮时无法设置 UINavigationBar 标题或将 UIButtons 放置在 UINavigationBar 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56094662/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com