【问题标题】:How to implement toolBar in tabBar?如何在tabBar中实现toolBar?
【发布时间】:2021-10-08 21:38:52
【问题描述】:

我想在我的标签栏中实现一个工具栏,或者像在照片应用中一样结束我的标签栏。我给你看:

Tab Bar 轻按选择按钮时转向 Tool bar

我已经尝试将工具栏放在 Tab bar 控制器视图中,但没有成功。

感谢您的回复!

【问题讨论】:

    标签: ios swift uitabbarcontroller uitoolbar


    【解决方案1】:

    一个简单的解决方案是在某些事件上隐藏 tabBar(照片应用程序有一个按钮可以做到这一点)。然后创建一个自定义 ToolBar 并将其添加为视图控制器的子视图。

    【讨论】:

    • 感谢您的回复,我解释一下我是如何使它成为 Demented07 的例子
    【解决方案2】:

    我相信这可能是您正在寻找的那种行为。您不必使用自定义UIButton,您可以使用UIBarButtonItem,但这取决于您,我只是选择使用自定义UIButton 来演示它,以向您展示这是一种方法。

    class ViewController: UIViewController {
        var selectButton: UIButton!
        var isInSelectMode = false
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            selectButton = UIButton(type: .system)
            selectButton.addTarget(self, action: #selector(selectButtonTapped), for: .touchUpInside)
            selectButton.setTitle("Select", for: .normal)
            view.addSubview(selectButton)
            
            let trashButton = UIBarButtonItem(barButtonSystemItem: .trash, target: nil, action: nil)
            toolbarItems = [trashButton]
            
            selectButton.translatesAutoresizingMaskIntoConstraints = false
            NSLayoutConstraint.activate([
                selectButton.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor, constant: -15),
                selectButton.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: 15),
            ])
            
        }
        
        @objc func selectButtonTapped() {
            isInSelectMode = !isInSelectMode
            if isInSelectMode {
                selectButton.setTitle("Done", for: .normal)
                tabBarController?.tabBar.isHidden = true
                tabBarController?.tabBar.backgroundColor = .systemBackground
                navigationController?.setToolbarHidden(false, animated: true)
                
            } else {
                selectButton.setTitle("Select", for: .normal)
                tabBarController?.tabBar.isHidden = false
                tabBarController?.tabBar.backgroundColor = .clear
                navigationController?.setToolbarHidden(true, animated: true)
                
            }
            
        }
    
    }
    

    它是这样的:

    【讨论】:

    • 谢谢你的例子,我找到了如何处理它。我使用没有导航控制器的工具栏,我把它放在标签栏上,并将布局更改为 Autoresizing Mask。
    猜你喜欢
    • 2016-05-20
    • 2020-06-14
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 2018-11-17
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多