【问题标题】:Hide UIButton on custom TabBar when TabBar is hidden隐藏 TabBar 时在自定义 TabBar 上隐藏 UIButton
【发布时间】:2021-06-04 04:02:46
【问题描述】:

我开发了带有居中 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
    }
}

如何隐藏按钮?以及如何在下方放置toolBar?

【问题讨论】:

    标签: uibutton uitabbarcontroller uitoolbar


    【解决方案1】:

    我解决了这个问题:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 1970-01-01
      • 2011-09-10
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多