【问题标题】:Button Doesn't Work on iPhone X or XR But Works on Simulator按钮在 iPhone X 或 XR 上无效,但在模拟器上有效
【发布时间】:2020-10-03 00:43:44
【问题描述】:

我有一个按钮,点击后会变成复选标记。它适用于包括 X 和 XR 在内的所有模拟器,但不适用于实际的 X 或 XR 设备(根据使用 TestFlight 的朋友的说法)。不过,它适用于其他几种设备。想知道是否与 X 和 XR 有什么特别的关系,或者可能是限制,剪辑到界限?问题是它没有显示复选标记或将与其单元格关联的值添加到我拥有的数组中,所以我认为它在这些手机上是不可选的。

let button: UIButton = {
    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.layer.masksToBounds = true
    button.isUserInteractionEnabled = true
    button.clipsToBounds = true
    button.contentMode = .scaleAspectFit
    return button
}()

约束

    button.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    button.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    button.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -(width/40)).isActive = true
    button.widthAnchor.constraint(equalToConstant: width/8).isActive = true

手势识别器

private func setupGestureRecognizer() {
    let joinButtonTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(joinButtonTapped))
    button.addGestureRecognizer(joinButtonTapGestureRecognizer)
}

@objc private func joinButtonTapped() {
    delegate?.joinButtonTapped(inCell: self, button: button)
}

JoinButtonTapped -> 加号按钮到复选标记并将组名添加到数组

extension SearchGroupsVC: GroupsViewCellDelegate {
    func joinButtonTapped(inCell cell: UITableViewCell, button: UIButton) {
        guard let indexPath = tableView.indexPath(for: cell) else { return }
        if searchBar.text?.count != 0 {
            group = filteredTypes[indexPath.row]
        } else {
            group = allGroups[indexPath.row]
        }
        if selected.contains(group) {
            if self.traitCollection.userInterfaceStyle == .dark {
                button.setImage(UIImage(named: "plus_white"), for: .normal)
            } else {
                button.setImage(UIImage(named: "plus_black"), for: .normal)
            }
            selected.remove(at: selected.index(of: group)!)
        } else if !selected.contains(group){
            if self.traitCollection.userInterfaceStyle == .dark {
                button.setImage(UIImage(named: "checkmark_white"), for: .normal)
            } else {
                button.setImage(UIImage(named: "checkmark_black"), for: .normal)
            }
            selected.append(group)
        }
    }
}

【问题讨论】:

  • 您是否收到按钮的回调? (就像#selector 被调用?)
  • 不幸的是,我没有办法在手头的 X 或 XR 上对其进行测试。这是一些朋友在试用 TestFlight 版本时告诉我的。我只有 iPhone 7 和 Xcode 模拟器可以尝试,但这些都可以。
  • 等等——为什么要在按钮上添加轻击手势识别器?
  • 哦,错了吗?不确定我是否看到在教程中使用它。 Tap Tester 代码位于自定义视图单元格中。它适用于大多数手机。
  • 一个按钮已经可以点击了,那么点击手势识别器是干什么用的?

标签: swift uibutton


【解决方案1】:

不确定这是否可行,但您应该使用addTarget 而不是手势识别器:

// let joinButtonTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(joinButtonTapped))
// button.addGestureRecognizer(joinButtonTapGestureRecognizer)
// remove this ^

button.addTarget(self, action: #selector(joinButtonTapped), for: .touchUpInside)

【讨论】:

  • 您是否认为使用手势识别器的新版本 iPhone 会破坏功能,所以我应该使用 addTarget?
  • 您应该请您的朋友发送一些屏幕截图。但是,是的,您应该始终在 UIButtons 上使用 addTarget
  • 他们给我看了。当您触摸它时,它甚至不会像普通按钮那样突出显示。
  • 可能是因为点击手势吃掉了媒体......另外,请尝试let button = UIButton(type: .system) 以确保您获得系统突出显示行为:stackoverflow.com/a/41401769/14351818
猜你喜欢
  • 2017-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多