Simple tvOS UIButton not working

I am trying to implement a simple UIButton (tvOS and Swift), but the TouchDown event does not fire. (also tried TouchUpInside).

  • In the simulator, I see visually that the button is pressed.

my ViewController.swift code is:

import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let button = UIButton(type: UIButtonType.System) button.frame = CGRectMake(100, 100, 400, 150) button.backgroundColor = UIColor.greenColor() button.setTitle("Test", forState: UIControlState.Normal) button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchDown) self.view.addSubview(button) self.view.userInteractionEnabled = true } func buttonAction(sender:UIButton!){ NSLog("Clicked") } } 

What do I need to do to detect a button click?

+5
source share
1 answer

You should not use ".TouchDown" or ".TouchUpInside" on tvOS, as this does not do what you can expect: instead, you most likely want to use "UIControlEvents.PrimaryActionTriggered".

TouchUpInside is caused by valid touches, which is actually not the case. If you want the Select button to be pressed on the remote control to start your button, you must use PrimaryActionTriggered.

+23
source

Source: https://habr.com/ru/post/1238423/


All Articles