swift UITabBarController issue - something(UINavigationBarContentView?)覆盖子UIViewController上的按钮,使按钮无法按下

avwztpqn  于 2023-06-28  发布在  Swift
关注(0)|答案(1)|浏览(170)

我实现了UITabBarController,其中有一个由UIViewController组成的选项卡。它由两部分组成- UIView和UIButton,以及UITableView。我没有使用UITableViewController,因为它不可能添加自定义按钮到它的标题:

final class ContactsController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@objc func buttonTapped(sender: UIButton) {
        print("Button tapped!")
    }

lazy var addButton = { [weak self] in
    guard let self = self else { return UIButton()}
    let button = UIButton(type: .custom)
    button.backgroundColor = .systemBlue
    button.tintColor = .white
    button.layer.cornerRadius = 8
    button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
    return button
}()

lazy var buttonBarView = {
    let barView = UIView()
    barView.clipsToBounds = true
    // barView.isUserInteractionEnabled = true
    return barView
}()

lazy var tableView = {
    let tableView = UITableView()
    return tableView
}()
    
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 50 }

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.register(ContactCell.self)
    
    addButton.setTitle("Add", for: .normal)
    tableView.dataSource = self
    tableView.delegate = self

    view.backgroundColor = .systemBackground
    title = "Contacts"
    tableView.rowHeight = 125
    
    self.view.bringSubviewToFront(addButton)

    setUpViews()
}

private func setUpViews() {
    
    self.view.addSubview(buttonBarView)
    buttonBarView.addConstraints(
        .alignTopWithTop(of: self.view),
            .alignLeadingWithLeading(of: view),
            .alignWidth(with: view, mult: 1),
        .alignHeight(with: view, mult: 0.045)
        )
    
    buttonBarView.addSubview(addButton)
    addButton.addConstraints(
               .alignTopWithTop(of: buttonBarView),
               .alignBottomWithBottom(of: buttonBarView),
               .alignTrailingWithTrailing(of: buttonBarView, const: 8),
               .setAspectWidth(ratio: 1))
    
    
    self.view.addSubview(tableView)
    tableView.addConstraints(
        .alignTopWithBottom(of: buttonBarView),
        .alignLeadingWithLeading(of: view),
        .alignWidth(with: view, mult: 1),
        .alignBottomWithBottom(of: view)
    )

请忽略与setUpViews函数结合的lazy var-这是因为我花了几个小时试图调试它。
一切看起来都很好,但有一个问题-按钮“...”无法按下:

如果我改变了限制,并将其移动到下面的“联系人”标题,它可以按下。
我使用了调试视图工具,并注意到可能是UINavigationBarContentView覆盖了按钮(此处已选中):

我试图通过代码访问它,但它是不可访问的。
另外,我使用了view.bringSubviewToFront()将按钮移到前面,但它没有改变任何东西。
我打印了UIViewController的所有子视图,并对每个子视图都做了同样的操作。但印刷品只向我展示了我创造的那些视图。
请帮助解决此问题。我检查了UIViewController上的Apple文档,但不幸的是,没有找到任何有用的东西。
.toolBarItems属性与UINavigationController一起使用,所以我认为它不能在我的情况下使用。
谢谢你。

zxlwwiss

zxlwwiss1#

问题是你使用了buttonBarView。您将其约束设置为位于视图控制器视图的最顶部。但是那个视图的顶部在导航栏后面。这正是您在视图层次结构调试视图中看到的。
最简单的解决方案是删除buttonBarViewUINavigationBar已经提供了向导航栏添加按钮(和视图)的方法。这通过UIViewController API-navigationItem完成。
您甚至不需要将addButton作为UIButton。使用标准+创建标准UIBarButtonItem

let btnAdd = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
navigationItem.rightBarButtonItem = btnAdd

如果你不想要标准的添加图标,有一些方法可以创建带有标题或图像的UIBarButtonItem。如果你真的想使用你的自定义按钮,那么你可以创建一个UIBarButtonItem按钮作为自定义视图:

let btnAdd = UIBarButtonItem(customView: addButton)
navigationItem.rightBarButtonItem = btnAdd

不需要额外的视图或约束。

相关问题