我通过 Swift在tvOS中创建了一个UIButton let randomBtn = UIButton()randomBtn.setTitle("Zufällig", forState: .Normal)let RndNormal = UIImage(named: "RndNormal")let RndHoover = UIImage(named: "RndHoover")randomBtn.setImage(RndNormal,
let randomBtn = UIButton() randomBtn.setTitle("Zufällig", forState: .Normal) let RndNormal = UIImage(named: "RndNormal") let RndHoover = UIImage(named: "RndHoover") randomBtn.setImage(RndNormal, forState: .Normal) randomBtn.setImage(RndHoover, forState: .Focused) randomBtn.setTitleColor(UIColor.blackColor(), forState: .Normal) randomBtn.setTitleColor(UIColor.whiteColor(), forState: .Focused) randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents.TouchUpInside) let screenSize: CGRect = UIScreen.mainScreen().bounds let screenWidth = screenSize.width randomBtn.frame = CGRect(x: screenWidth - 150, y: 60 , width: 70 , height: 70) self.view.addSubview(randomBtn)
但如果按下按钮,动作永远不会被解雇,tvOS有什么不同吗?
func click(sender: UIButton) { print("click") }在tvOS for Button动作UIControlEvents中,TouchUpInside不会调用.
您必须使用如下所示的UIControlEvents PrimaryActionTriggered.
randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents. PrimaryActionTriggered)
如果您有任何疑惑,请参阅此链接
https://forums.developer.apple.com/thread/17925