隐藏 TabBar 时隐藏自定义 TabBar 上的 UIButton

时间:2021-03-05 13:06:25

标签: uibutton uitabbarcontroller uitoolbar

我开发了带有居中 UIButton 的自定义 tabBarController。在某些视图中,我需要隐藏 tabBar,但 UIButton 不隐藏。

My custom tabBar

When I hide tabBar

这是我在 tabBarController 类上的代码:

let addButton = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 60))

// MARK: - Life Cycle
override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self
    
    self.setupTabBar()
    self.setupAddButton()
}

// MARK: - Methods
private func setupTabBar() {
    let sectionTabNavigationController = createNavController(
        vc: MSMainCatalogVC(),
        title: NSLocalizedString("Catalog", comment: ""),
        image: UIImage(systemName: "text.justify"))
    
    let recentTabNavigationController = createNavController(
        vc: MSNotesTableVC(),
        title: NSLocalizedString("Recent", comment: ""),
        image: UIImage(systemName: "clock"))
    
    viewControllers = [recentTabNavigationController,
                       sectionTabNavigationController]
}

private func createNavController(vc: UIViewController,
                                 title: String,
                                 image: UIImage?) -> UINavigationController {
    let navController = UINavigationController(rootViewController: vc)
    navController.tabBarItem.title = title
    if let image = image {
        navController.tabBarItem.image = image
    }
    
    return navController
}

private func setupAddButton() {
        var addButtonFrame = addButton.frame
        addButtonFrame.origin.y = self.view.bounds.height - self.tabBar.bounds.height * 2
        addButtonFrame.origin.x = view.bounds.width/2 - addButtonFrame.size.width/2
        addButton.frame = addButtonFrame
        
        view.addSubview(addButton)
        
        addButton.setImage(UIImage(systemName: "plus"), for: .normal)
        addButton.backgroundColor = .systemGray6
        addButton.tintColor = UIColor.systemGray2
        addButton.layer.borderColor = UIColor.systemGray2.cgColor
        addButton.layer.borderWidth = 1
        addButton.layer.cornerRadius = 30
        
        addButton.addTarget(self, action: #selector(pressCenterButton(sender:)), for: .touchUpInside)
        
        view.layoutIfNeeded()
}

// MARK: - Actions
@objc private func pressCenterButton(sender: UIButton) {
    // Show alert when add button was tapped
    MSAddAlert.shared.show(for: self,
                           button1Title: NSLocalizedString("Make photo", comment: ""),
                           button2Title: NSLocalizedString("Select photo", comment: ""),
                           button3Title: NSLocalizedString("Make note", comment: ""),
                           button4Title: NSLocalizedString("Select file", comment: ""),
                           button1Action: self.makePhoto,
                           button2Action: self.addPhoto,
                           button3Action: self.goToAddNoteVC,
                           button4Action: nil)
}

private func goToAddNoteVC() {
    self.selectedViewController?.show(MSAddNoteVC(), sender: nil)
}

下面是我在第二个视图中隐藏 tabBar 的方式:

private func setupBars() {
    self.navigationItem.title = self.title ?? NSLocalizedString("New note",
                                                                comment: "")
    self.tabBarController?.tabBar.alpha = 0
    self.navigationController?.setToolbarHidden(false,
                                                animated: true)
    if #available(iOS 14.0, *) {
        self.setToolbarItems([self.cancelButton,
                              UIBarButtonItem(systemItem: .flexibleSpace),
                              self.saveButton],
                             animated: true)
    } else {
        // Fallback on earlier versions
    }
}

如何隐藏按钮?以及如何将工具栏放置在下方?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题:

我将 TabBarController 重写为 Singleton 并实现了 func:

func hideAddButton() {
    self.addButton.isHidden = true
}

当我想隐藏 tabBar 时,我会在我的 ViewControllers 上调用这个方法。 我在 TabBarController 上的 viewWillAppear 上添加了 isHidden = false 以再次显示它。