How to create a UIButton programmatically - swift

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.

Related

swift can't find textfield in scope even when its in the same viewController

I need to print value of txtField after clicking the button, txtField is on the viewcotroller but xcode returns an error cannot find 'txtField' in scope but they are in the same view
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.backgroundColor = .green
button.setTitle("Test Button", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
var txtField: UITextField = UITextField()
txtField.frame = CGRect(x: 50, y: 70, width: 200, height: 30)
txtField.backgroundColor = UIColor.gray
self.view.addSubview(txtField)
}
#objc func buttonAction(sender: UIButton!) {
print(txtField.text)
}
Actually, no, it's not in scope. Your var txtField is inside the viewDidLoad function. No other function's code can see inside this function, so the variable is not in scope from within another function such as your buttonAction.
In general the rule that things inside a scope can see only things that at a higher level of scope. If var txtField appeared outside both viewDidLoad and buttonAction (i.e. an instance property), then code inside both would be able to see it.
It's an easy move to make:
var txtField: UITextField = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.backgroundColor = .green
button.setTitle("Test Button", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
txtField.frame = CGRect(x: 50, y: 70, width: 200, height: 30)
txtField.backgroundColor = UIColor.gray
self.view.addSubview(txtField)
}
#objc func buttonAction(sender: UIButton!) {
print(txtField.text)
}
The error is that are in 2 different function, override or #objc doesn't change this simple thing. So move the txtField initialization outside them so can be readed or written by both.
var txtField: UITextField = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.backgroundColor = .green
button.setTitle("Test Button", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
txtField.frame = CGRect(x: 50, y: 70, width: 200, height: 30)
txtField.backgroundColor = UIColor.gray
self.view.addSubview(txtField)
}
#objc func buttonAction(sender: UIButton!) {
print(txtField.text)
}
In your code txtField is declared in viewDidLoad on line
var txtField: UITextField = UITextField()
Therefore out of the scope of buttonAction.
To mitigate this place
txtField
out of scope as a instance variable

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)

Implementing the same function for different objects

Newbie coder and learning Swift. I want the function to be applicable for both UIButtons and couldn't figure out how to make it happen for second one.
private lazy var boostButton: UIButton = {
let button = UIButton(type: .custom)
button.frame = CGRect(x: 10, y: 10, width: 80, height: 80)
button.setImage(UIImage(named: "simsek.png"), for: .normal)
button.imageView?.contentMode = .scaleAspectFit
button.contentHorizontalAlignment = .center
button.contentVerticalAlignment = .center
button.layer.cornerRadius = 0.5 * button.bounds.size.width
button.layer.masksToBounds = true
button.addTarget(self, action: #selector(touchDown), for: [.touchDown, .touchDragEnter])
button.addTarget(self, action: #selector(touchUp), for: [.touchUpInside, .touchDragExit, .touchCancel])
return button
}()
private lazy var informationButton: UIButton = {
let button = UIButton(type: .custom)
button.frame = CGRect(x: 120, y: 10, width: 35, height: 35)
button.setImage(UIImage(named: "yenigozlukgri.png"), for: .normal)
button.imageView?.contentMode = .scaleAspectFit
button.contentHorizontalAlignment = .center
button.contentVerticalAlignment = .center
button.layer.cornerRadius = 0.5 * button.bounds.size.width
button.layer.masksToBounds = true
button.addTarget(self, action: #selector(touchDown), for: [.touchDown, .touchDragEnter])
button.addTarget(self, action: #selector(touchUp), for: [.touchUpInside, .touchDragExit, .touchCancel])
return button
}()
These are my buttons. I don't use storyboard but I believe that's not essential for the solution.
#objc func touchDown() {
animator.stopAnimation(true)
boostButton.backgroundColor = .red
//highlightedColor
}
#objc func touchUp() {
animator = UIViewPropertyAnimator(duration: 0.5, curve: .easeOut, animations: {
self.boostButton.backgroundColor = .gray
})
animator.startAnimation()
}
What I want to do is, when one of the buttons are clicked, it should perform the animation. If I add informationButton like the boostButton to my functions, both of them perform the animation even though one button is clicked. It should work for just the clicked one. How can I fix it to be functional for even more buttons ?
Use the parameter
#objc func touchDown(_ sender:UIButton) {
animator.stopAnimation(true)
sender.backgroundColor = .red
//highlightedColor
}
#objc func touchUp(_ sender:UIButton) {
animator = UIViewPropertyAnimator(duration: 0.5, curve: .easeOut, animations: {
sender.backgroundColor = .gray
})
animator.startAnimation()
}

Swift4 addTarget at UIButton doesnt work in UIView

I programmatically created an UIButton and added it to a subview. AddTarget doesnt work there though. AddTarget only works if I add the button to the mainview.
self.view.addSubview(button)
instead of
ViewSystem.addSubview(button)
Doesnt anyone know why?
Here is the fullcode:
class ViewController: UIViewController {
var ViewSystem = UIView()
#objc func TestPressed(sender: UIButton?) {Test.text=String((sender?.tag)!)
func ButtonCreate () {
let button = UIButton()
button.frame = CGRect(x: 50, y: 100, width: 70, height: 70)
button.addTarget(self, action: #selector(TestPressed), for: .touchUpInside)
button.backgroundColor = UIColor.red
button.tag=5
ViewSystem.addSubview(button)
self.view.addSubview(ViewSystem)
}
}
This happening because you are set your button frame graterthen your view that's why your button not tapped.
you are not set your view frame and then how you can you set your button inside your view.
Here I update your ButtonCreate () function code it's working nicely.
func ButtonCreate () {
ViewSystem.frame = CGRect(x: 50, y: 100, width: 200, height: 70)
ViewSystem.backgroundColor = .blue
let button = UIButton()
button.frame = CGRect(x: 0, y: 0, width: 70, height: 70)
button.addTarget(self, action: #selector(TestPressed), for: .touchUpInside)
button.backgroundColor = UIColor.red
button.tag = 5
ViewSystem.clipsToBounds = false
ViewSystem.addSubview(button)
self.view.addSubview(ViewSystem)
}
I hope it's helpfull for you and save your time
You have to give frame to your ViewSystem. and ViewSystem's height width should be greater than button's X and Y.
var ViewSystem = UIView()
ViewSystem.frame = CGRect(x: 50, y: 100, width: 70, height: 70)
#objc func TestPressed(sender: UIButton?) {Test.text=String((sender?.tag)!)
func ButtonCreate () {
let button = UIButton()
button.frame = CGRect(x: 0, y: 0, width: 70, height: 70)
button.addTarget(self, action: #selector(TestPressed), for: .touchUpInside)
button.backgroundColor = UIColor.red
button.tag=5
ViewSystem.addSubview(button)
self.view.addSubview(ViewSystem)
}
}

tvOS Button Action

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