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.
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];
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)
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
}
}
programmatically tap UIButton swift
have you tried:
myAutomatedButton.sendActionsForControlEvents(.TouchUpInside)
This appears to be a previous question and a possible duplicate, but the post I found was: how to programmatically fake a touch event to a UIButton?
+1 for swift 3/4 below: @Eneko Alonso
Swift add show action to button programmatically
You can go for below code
`
let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
btn.backgroundColor = UIColor.green
btn.setTitle("Click Me", for: .normal)
btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btn.tag = 1
self.view.addSubview(btn)
for action
@objc func buttonAction(sender: UIButton!) {
let btnsendtag: UIButton = sender
if btnsendtag.tag == 1 {
dismiss(animated: true, completion: nil)
}
}
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
Change the Uitableviewcell Height According to Amount of Text
How to Reset the iOS Simulator from the Command Line
How to Create a Scroll View with a Page Control Using Swift
Check If a Uiscrollview Reached the Top or Bottom
Loading an Image into Uiimage Asynchronously
iOS 8 Enabled Device Not Receiving Push Notifications After Code Update
Location Services Not Working in iOS 11
App Rejected Because of "Missing Push Notification Entitlement"
Required Ipv6 Compatibility - iOS App Rejected by Apple
Attempt to Insert Non-Property List Object When Trying to Save a Custom Object in Swift 3
Navigation Bar Rightbaritem Image-Button Bug iOS 11
How to Get Unique Id in iOS Device
No Avplayer Delegate? How to Track When Song Finished Playing? Objective C iPhone Development
How to Create Managedobjectcontext Using Swift 3 in Xcode 8