tvOS Button Action - swift

I create a UIButton in tvOS via Swift
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 the action never get fired if I press the button, is there anything different in tvOS?
func click(sender: UIButton) {
print("click")
}

In tvOS for Button action UIControlEvents TouchUpInside will not call.
You have to use UIControlEvents PrimaryActionTriggered like below.
randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents. PrimaryActionTriggered)
Also Refer this link if you have any confusion
https://forums.developer.apple.com/thread/17925

randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents.PrimaryActionTriggered)
Solved it, its just a other UIControlEvents

Related

Swift/Xcode - No response from button in custom annotation view

I have created a custom annotation view, as described here: https://sweettutos.com/2016/03/16/how-to-completely-customise-your-map-annotations-callout-views/
It works fine and displays the correct data/info.
The problem seems to be that there is no respons to .touchUpInside from the added button.
let button = UIButton()
button.addTarget(self, action: #selector(ViewController.deleteFromAnnot(sender: )), for: .touchUpInside)
button.frame = CGRect.init(x: 16, y: 543, width: 268, height: 30)
button.backgroundColor = .yellow
calloutView.addSubview(button)
#objc func deleteFromAnnot(sender: UIButton)
{
print("Do something")
}
It looks to me like you have set your button target incorrectly.
Replace
button.addTarget(self, action: #selector(ViewController.deleteFromAnnot(sender: )), for: .touchUpInside)
With
button.addTarget(self, action: #selector(deleteFromAnnot(sender: )), for: .touchUpInside)

The action in addTarget of my button does not work

Xcode did not report errors but nothing was printed when I pressed the button.
I tried it with action:
#selector (print), action: #selector (self.print), action: #selector (ViewController.print)..etc..
Swift 4, Xcode 10.1
class SomeViewController:UIViewController {
var button:UIButton = UIButton()
func setupButton()
{
button = UIButton(frame: CGRect(x: 140, y: 140, width: 90, height: 40))
button.addTarget(self, action: #selector(print(-:)) , for: .touchUpOutside)
navigationController?.navigationBar.addSubview(button)
button.setTitle("Convert", for: .normal)
button.backgroundColor = .yellow
view.addSubview(button)
}
#objc func print(_sender: UIButton)
{
print("Stackoverflow")
}
override func viewDidLoad()
{
super.viewDidLoad()
view.addSubview(CollectionView)
CollectionView.frame = view.frame
setupButton()
}
}
You just have to change event type from .touchUpOutside to .touchUpInside
Here is events documentation
Change this ( You need touchUpInside instead of touchUpOutside )  
button.addTarget(self, action: #selector(print(_:)) , for: .touchUpOutside)
to
button.addTarget(self, action: #selector(printRes) , for: .touchUpInside)
#objc func printRes(_ sender: UIButton) { }
Also don't use print as button action name because it's a reserved method
Also to get the button click action set it's type
button = UIButton(type: .system)
button.frame = CGRect(x: 140, y: 140, width: 90, height: 40)

Custom Bar Button with Image And Title

I got the problem when i use Custom Bar button with image and title, I use below code
let button = UIButton(type: .system)
button.titleEdgeInsets.left = 5
button.setImage(buttonImg, for: .normal)
button.setTitle(title, for: .normal)
button.sizeToFit()
button.addTarget(self, action: #selector(self.popVC), for: .touchUpInside)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
And Here is the problem snippet. Text not show clearly
Try this:
let backButton = UIButton(type: .system)
backButton.frame = CGRect(x: 0, y: 0, width: 500, height: 30)
backButton.setImage(buttonImg, for: .normal)
backButton.setTitle(title, for: .normal)
backButton.addTarget(self, action: #selector(self.popVC(_:)), for: .touchUpInside)
backButton.sizeToFit()
let backBarButton = UIBarButtonItem(customView: backButton)
self.navigationItem.leftBarButtonItem = backBarButton

How to create a UIButton programmatically

I am trying to build UIViews programmatically. How do I get a UIButton with an action function in Swift?
The following code does not get any action:
let btn: UIButton = UIButton(frame: CGRectMake(100, 400, 100, 50))
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("Click Me", forState: UIControlState.Normal)
btn.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(buttonPuzzle)
The following selector function is:
func buttonAction(sender: UIButton!) {
var btnsendtag: UIButton = sender
}
You're just missing which UIButton this is. To compensate for this, change its tag property.
Here is you answer:
let btn: UIButton = UIButton(frame: CGRectMake(100, 400, 100, 50))
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("Click Me", forState: UIControlState.Normal)
btn.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
btn.tag = 1 // change tag property
self.view.addSubview(btn) // add to view as subview
Swift 3.0
let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
btn.backgroundColor = UIColor.green
btn.setTitle(title: "Click Me", for: .normal)
btn.addTarget(self, action: #selector(buttonAction), forControlEvents: .touchUpInside)
btn.tag = 1
self.view.addSubview(btn)
Here is an example selector function:
func buttonAction(sender: UIButton!) {
var btnsendtag: UIButton = sender
if btnsendtag.tag == 1 {
//do anything here
}
}
Using a tag is a fragile solution. You have a view and you are creating and adding the button to that view, you just need to keep a reference to it: For example
In your class, keep a reference to the button
var customButton: UIButton!
Create the button and set the reference
let btn = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
btn.backgroundColor = .greenColor()
btn.setTitle("Click Me", forState: .Normal)
btn.addTarget(self, action: #selector(MyClass.buttonAction), forControlEvents: .TouchUpInside)
self.view.addSubview(btn)
customButton = btn
Test against this instance in the action function
func buttonAction(sender: UIButton!) {
guard sender == customButton else { return }
// Do anything you actually want to do here
}
You have to addSubview and tag on that btn.

How to scale image and center it on a UIButton in Swift?

var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
var image = UIImage(named: "myimage.png") as UIImage!
button.frame = CGRectMake(0, 0, 100, 100)
button.setImage(image, forState: .Normal)
I have an image that I've set on my button, but I'd like to scale it to be smaller than the button (for example the image should be 50,50) and center in the button. How might I do this in Swift?
Xcode 8.3.1 • Swift 3.1
let button = UIButton(type: .custom)
let image = UIImage(named: "myimage.png")
func buttonTouchDown(_ button: UIButton) {
print("button Touch Down")
}
override func viewDidLoad() {
super.viewDidLoad()
button.frame = CGRect(x: 0, y: 0 , width: 100, height: 100)
button.backgroundColor = .clear
button.addTarget(self, action: #selector(buttonTouchDown), for: .touchDown)
button.setTitle("Title", for: .normal)
button.setTitleColor(.black, for: .normal)
button.setImage(image, for: .normal)
button.imageEdgeInsets = UIEdgeInsetsMake(25,25,25,25)
view.addSubview(button)
}
Swift 5, Swift 4, Swift 3
var image = UIImage(named: "myimage") as UIImage!
btnetc.setImage(image, for: .normal)
btnetc.imageView?.contentMode = .scaleAspectFill
or
btnetc.imageView?.contentMode = .scaleAspectFit
or
btnetc.imageView?.contentMode = .scaleToFill
Ther is a much easier way to do this where you don't have to deal with content insets. Here its is:
var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
var image = UIImage(named: "myimage.png") as UIImage!
button.frame = CGRectMake(0, 0, 100, 100)
button.setImage(image, forState: .Normal)
button.contentMode = .center
button.imageView?.contentMode = .scaleAspectFit
Or via XIB after selecting your UIButton.
Just make sure your insets are all the same.
Xcode 13, Swift 5
Using storyboard, select the button, then in the size inspect click the dropdown on size just above Content inset. There is a list of sizes to select from, and this will adjust your image size(if you use system image). The default size is already set when you added the button on the storyboard to the View Controller.