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)
But if the button is pressed, the action will never be fired, is there any difference in tvOS?
func click(sender: UIButton) {
print("click")
}
You must use UIControlEvents PrimaryActionTriggered as shown below.
randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents. PrimaryActionTriggered)
If you have Any doubts, please refer to this link
https://forums.developer.apple.com/thread/17925
I use Swift in tvOS Created a UIButton
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 )
But if the button is pressed, the action will never be fired, is there any difference in tvOS?
func click(sender: UIButton) {
print("click")
}
p>
In tvOS for Button action UIControlEvents, TouchUpInside will not be called.
You must use UIControlEvents PrimaryActionTriggered as shown below.
< pre>randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents. PrimaryActionTriggered)
If you have any doubts, please refer to this link
< p>https://forums.developer.apple.com/thread/17925