【发布时间】:2021-10-08 21:38:52
【问题描述】:
我想在我的标签栏中实现一个工具栏,或者像在照片应用中一样结束我的标签栏。我给你看:
我已经尝试将工具栏放在 Tab bar 控制器视图中,但没有成功。
感谢您的回复!
【问题讨论】:
标签: ios swift uitabbarcontroller uitoolbar
我想在我的标签栏中实现一个工具栏,或者像在照片应用中一样结束我的标签栏。我给你看:
我已经尝试将工具栏放在 Tab bar 控制器视图中,但没有成功。
感谢您的回复!
【问题讨论】:
标签: ios swift uitabbarcontroller uitoolbar
一个简单的解决方案是在某些事件上隐藏 tabBar(照片应用程序有一个按钮可以做到这一点)。然后创建一个自定义 ToolBar 并将其添加为视图控制器的子视图。
【讨论】:
我相信这可能是您正在寻找的那种行为。您不必使用自定义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)
}
}
}
【讨论】: