How to Create a Basic Uibutton Programmatically

How do I create a basic UIButton programmatically?

Here's one:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

How to create a UIButton programmatically

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
}
}

Make a UIButton programmatically in Swift

You're just missing the colon at the end of the selector name. Since pressed takes a parameter the colon must be there. Also your pressed function shouldn't be nested inside viewDidLoad.

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let myFirstLabel = UILabel()
let myFirstButton = UIButton()
myFirstLabel.text = "I made a label on the screen #toogood4you"
myFirstLabel.font = UIFont(name: "MarkerFelt-Thin", size: 45)
myFirstLabel.textColor = .red
myFirstLabel.textAlignment = .center
myFirstLabel.numberOfLines = 5
myFirstLabel.frame = CGRect(x: 15, y: 54, width: 300, height: 500)
myFirstButton.setTitle("✸", for: .normal)
myFirstButton.setTitleColor(.blue, for: .normal)
myFirstButton.frame = CGRect(x: 15, y: -50, width: 300, height: 500)
myFirstButton.addTarget(self, action: #selector(pressed), for: .touchUpInside)
}

@objc func pressed() {
var alertView = UIAlertView()
alertView.addButtonWithTitle("Ok")
alertView.title = "title"
alertView.message = "message"
alertView.show()
}

EDIT: Updated to reflect best practices in Swift 2.2. #selector() should be used rather than a literal string which is deprecated.

Creating UIButton programmatically in Swift 4 but getting error

you cannot addTarget or call any method in the class space , this space is for declartion as the error suggests .

to fix this you can do either this

let button:UIButton =
{
let btn = UIButton()
btn.addTarget(self, action: #selector(buttonTapped),for: .touchUpInside)
return btn
}()

Or

   let button:UIButton = UIButton()

override init(frame: CGRect) {
super.init(frame: frame)
button.addTarget(self, action: #selector(buttonTapped),for: .touchUpInside)

}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

in both cases you must add @objc annotation to the buttonTapped function because selectors must refer to an @objc function .

so it will be like this

   @objc func buttonTapped(){}

Also you need to add this button to the view on order for it to be drawn to the screen .

view.addSubView(button)

Creating dynamic multiple UIButton programmatically

Use Horizontal StackView for that. In StackView you can add as much arranged subviews as you want and it will be automatically adjusting to the available space. Just setup constraints for StackView and rest will just work out of the box.

You can add your button to StackView using:

stackView.addArangedSubview(button)

To create button you can simple use for loop:

for i in 1...3 { //change 3 to your value
let button = UIButton()
stackView.addArangedSubview(button)
}


Related Topics



Leave a reply



Submit