UIToolBar在屏幕底部的正确位置

ogsagwnx  于 2022-10-23  发布在  Swift
关注(0)|答案(1)|浏览(146)

由于缺乏创建UIToolBar元素的实践,我不完全理解将UIToolBar放置在屏幕底部的最简洁的方法。现在,我的代码如下所示:

private lazy var toolBar: UIToolbar = {  
    let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
    toolBar.translatesAutoresizingMaskIntoConstraints = false
    let addNewNote = UIBarButtonItem(barButtonSystemItem: .compose, target: toolBar, action: nil)
    let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: toolBar, action: nil)
    toolBar.items = [spacer, addNewNote]

    return toolBar
}()

private func setUpConstraints() {
    NSLayoutConstraint.activate([
        toolBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
        toolBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
        toolBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
    ])
}

这会产生预期的结果,但是我觉得我缺少实现这个简单元素的更好方法。首先,一旦设备旋转,工具栏不会填满从前导到尾随的所有空间,其次,基于UIScreen.bound属性分配前导/尾随约束和宽度/高度似乎是不正确的,尽管在我将这个奇怪的框架分配给工具栏之前,我在控制台中看到了ToolBarElement的约束布局问题。现在这个问题已经没有了,但情况并没有变得更加明朗。
我的目标是从iOS原生笔记应用程序

克隆UIToolBar
所以,作为一名经验丰富的iOS开发人员(您当然是!)你建议我做什么?
祝您今天愉快!

7xllpg7q

7xllpg7q1#

如果我理解得很好,您可以使用autolayaout:
在控制器类下声明您的对象:

let toolBar: UIToolbar = {
//let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)) // remove this
    let toolBar = UIToolbar()
    toolBar.translatesAutoresizingMaskIntoConstraints = false
    let addNewNote = UIBarButtonItem(barButtonSystemItem: .compose, target: toolBar, action: nil)
    let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: toolBar, action: nil)
     toolBar.tintColor = .yourColor
    let toolBarTitleLabel = UILabel()
    toolBarTitleLabel.text = "150 Results"
    toolBarTitleLabel.sizeToFit()
    toolBarTitleLabel.backgroundColor = .clear
    toolBarTitleLabel.textColor = .white
    toolBarTitleLabel.textAlignment = .center
    let topBarButtonItemTitleLabel = UIBarButtonItem(customView: toolBarTitleLabel)
    toolBar.items = [spacer, topBarButtonItemTitleLabel, spacer, addNewNote]

    return toolBar
}()

let mylabel2: UILabel = {
    let label = UILabel()
    label.text = "Your results data text here..."
    label.textAlignment = .center
    label.textColor = .gray
    label.font = .systemFont(ofSize: 16, weight: .regular)
    label.translatesAutoresizingMaskIntoConstraints = false

    return label
}()

let dummyView: UIView = {
    let v = UIView()
    v.backgroundColor = .systemBlue
    v.translatesAutoresizingMaskIntoConstraints = false

    return v
}()

在view DidLoad Set Constraints中:

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .black

    view.addSubview(toolBar)
    NSLayoutConstraint.activate([
        toolBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
        toolBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
        toolBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
    ])

    view.addSubview(mylabel2)
    NSLayoutConstraint.activate([
        mylabel2.bottomAnchor.constraint(equalTo: toolBar.topAnchor),
        mylabel2.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 30),
        mylabel2.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -30),
        mylabel2.heightAnchor.constraint(equalToConstant: 50)
    ])

    view.addSubview(dummyView)
    NSLayoutConstraint.activate([
        dummyView.bottomAnchor.constraint(equalTo: mylabel2.topAnchor),
        dummyView.leadingAnchor.constraint(equalTo: mylabel2.leadingAnchor),
        dummyView.trailingAnchor.constraint(equalTo: mylabel2.trailingAnchor),
        dummyView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 30)
    ])
}

这就是结果:

相关问题