How do I programmatically create graphical elements (like a UIButton
) in Swift? I tried to create and add button into a view, but wasn't able to.
Ios – How to create a button programmatically
iosswiftuibutton
Related Topic
- Ios – How to create a UIAlertView in Swift
- Swift – Using a dispatch_once singleton model in Swift
- Ios – How to create UILabel programmatically using Swift
- Swift Beta performance: sorting arrays
- Ios – Loading/Downloading image from URL on Swift
- IOS app with framework crashed on device, dyld: Library not loaded, Xcode 6 Beta
- R – How Do You Upgrade From Eclipse Ganymede to Eclipse Galileo
- Swift – How to make a button have a rounded border in Swift
Best Answer
Here is a complete solution to add a
UIButton
programmatically with the targetAction.Swift 2.2
It is probably better to use
NSLayoutConstraint
rather thanframe
to correctly place the button for each iPhone screen.Updated code to Swift 3.1:
Updated code to Swift 4.2:
The above still works if
func buttonAction
is declaredprivate
orinternal
.