我有一个带有 UITabBarController
和几个 View 的项目。有点像这样:
在此项目中,通过点击 UIViewController
上的 Tab 按钮来调用选项卡栏 Controller 。我想去掉带有“标题”的后退按钮,并将其替换为“X”图标。点击时,“X”图标将关闭 UITabBarController
并返回到调用者。我一直在使用 UINavigationItem
在 UINavigationController
上执行此操作,但这在这种情况下似乎不起作用。我将 UINavigationItem
拖到 View 中,它允许这样做,但它不会显示在 View 上,并且我拖放到其上的任何 UIBarButtonItem
也不会出现。
有没有办法真正做到这一点?我什至可以保留现有的后退按钮,然后去掉“标题”
最佳答案
我在发布问题后就明白了。只需进行更多研究即可。
要解决此问题,请将以下内容添加到 UITabBarController
代码中。
override func viewDidLoad() {
super.viewDidLoad()
let buttonImage = UIImage(named: "back")
let leftButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(dismissTabBar))
leftButton.tintColor = UIColor.black
self.navigationItem.leftBarButtonItem = leftButton
}
@IBAction func dismissTabBar() {
self.navigationController?.popToRootViewController(animated: true)
}
这给了我一个带有我的图像的黑色后退按钮。当我点击它时,它会让我回到调用“UIViewController”。
关于swift - 在 UITabBarController 上使用 UINavigationItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54391243/