swift - 在 UITabBarController 上使用 UINavigationItem

标签 swift uitabbarcontroller uibarbuttonitem

我有一个带有 UITabBarController 和几个 View 的项目。有点像这样:

enter image description here

在此项目中,通过点击 UIViewController 上的 Tab 按钮来调用选项卡栏 Controller 。我想去掉带有“标题”的后退按钮,并将其替换为“X”图标。点击时,“X”图标将关闭 UITabBarController 并返回到调用者。我一直在使用 UINavigationItemUINavigationController 上执行此操作,但这在这种情况下似乎不起作用。我将 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/

相关文章:

ios - 将 tableViewCell 中的数据传递给另一个 VC 失败

swift - ASImageNode - 无法分配给属性 : 'image' is a get-only property

迁移到 Swift 2 后未显示 UICollectionView 项目

ios - TabBarController 返回 null

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

iphone - 为什么我用于 nav_bar 按钮的图像有白边?

iphone - UIBarButtonItem 正常状态和按下状态切换了吗?

swift - 动态调整自定义 xib 单元格的大小

swift - 如何将 FMDB 与 cocoapods 和 swift 一起使用?

iphone - 更改 MoreNavigationController 中的文本颜色