gpt4 book ai didi

ios - 如何在 Swift 3 中隐藏 TabBarController 上的自定义按钮?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:26:15 28 4
gpt4 key购买 nike

我有一个 UITabBarController,在 TabBar 中间有自定义按钮。但是如果我设置 hidesBottomBarWhenPushed = true,我会得到一个奇怪的行为。

enter image description here

我在 Swift 3 中以编程方式创建了 UITabBarController

这是我创建自定义中间按钮的代码:

func setupMiddleButton() {
let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 48, height: 48))

var menuButtonFrame = menuButton.frame
menuButtonFrame.origin.y = view.bounds.height - menuButtonFrame.height
menuButtonFrame.origin.x = view.bounds.width/2 - menuButtonFrame.size.width/2
menuButton.frame = menuButtonFrame

menuButton.layer.cornerRadius = menuButtonFrame.height/2
view.addSubview(menuButton)

menuButton.setImage(UIImage(named: "updatemoment"), for: .normal)
menuButton.addTarget(self, action: #selector(menuButtonAction), for: .touchUpInside)

view.layoutIfNeeded()
}

func menuButtonAction() {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UINavigationController = storyboard.instantiateViewController(withIdentifier: "NewPostID") as! UINavigationController

self.present(vc, animated: true, completion: nil)
print("segue success")
}

如何解决?我希望中间按钮保留在 BottomBar 中。

提前致谢。

最佳答案

我能够通过以下方式修复它:

在类中实例化菜单按钮:

let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 64, height: 64))

在同一个 Controller (TabBarController)中添加两个函数:

func hideTabBar() {
self.tabBar.isHidden = true
self.menuButton.isHidden = true
}

func showTabBar() {
self.tabBar.isHidden = false
self.menuButton.isHidden = false
}

然后,每当您需要隐藏或显示 tabBar 时,使用:

let tabBar = self.tabBarController as! InitialViewController
tabBar.showTabBar()

我目前在某些 Controller 的 viewWillAppear 和 viewWillDisappear 中使用它。

关于ios - 如何在 Swift 3 中隐藏 TabBarController 上的自定义按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43295951/

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