1

I have a button that when clicked turns into a checkmark. It works on all simulators including X and XR but not on an actual X or XR device (according to friends using TestFlight). It works on several other devices though. Wondering if there's anything to do with X and XR specifically or may be constraints, clips to bounds? The problem is it isn't showing the checkmark or adding the value associated with its cell to an array I have, so I don't think it's selectable on those phones.

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

Constraints

    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

Gesture Recognizer

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 -> Plus Button to Checkmark and adds group name to array

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)
        }
    }
}
Aza
  • 63
  • 10
  • Are you getting callback from the button? (like does the `#selector` get called?) – aheze Oct 03 '20 at 00:45
  • I don't have a way to test it unfortunately on X or XR at hand. It's what some friends have told me from trying TestFlight version. I only have an iPhone 7 and the Xcode simulators to try, but those all work. – Aza Oct 03 '20 at 00:53
  • Wait -- why are you adding a tap gesture recognizer to the button? – aheze Oct 03 '20 at 01:32
  • Oh is that wrong? Not sure I saw to use that in a tutorial. Tap Tester code is in the custom view cell. It works with most phones. – Aza Oct 03 '20 at 01:49
  • Generally you should either use `UIButton` *or* `UITapGestureRecognizer`, not both... they are kind of parallel, except `UIButton` gets you more touch events (like touch down, touch up, touch up outside). – aheze Oct 03 '20 at 01:52
  • 1
    A button is already tappable, so what is the tap gesture recognizer for? – matt Oct 03 '20 at 01:55
  • So I'd be able to recognize when the user pressed the button itself in the table view cell...should I be using the answer below (addTarget)? – Aza Oct 03 '20 at 02:00
  • Yes but I doubt that's the problem. It sounds more like the button is outside its superview. – matt Oct 03 '20 at 02:41

1 Answers1

0

Not sure if this will work, but you should use addTarget instead of a gesture recognizer:

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

button.addTarget(self, action: #selector(joinButtonTapped), for: .touchUpInside)
aheze
  • 24,434
  • 8
  • 68
  • 125
  • Do you think with newer versions of iPhones using gesture recognizer is messing functionality up so I should use addTarget? – Aza Oct 03 '20 at 02:05
  • You should ask your friends to send some screenshots. But yes, you should always use `addTarget` on `UIButton`s. – aheze Oct 03 '20 at 02:09
  • They showed me. It doesn't even get highlighted like a normal button when you touch it. – Aza Oct 03 '20 at 02:13
  • Could be because of the tap gesture eating the press... and also, try `let button = UIButton(type: .system)` to make sure you're getting the system highlight behavior: https://stackoverflow.com/a/41401769/14351818 – aheze Oct 03 '20 at 02:29