Buttons are of the UIButton class, so you can create buttons on your instance.
Download the background image of the button appropriately.
Put the above image in the Assets folder. You are now ready.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Make a button
let okButton=UIButton()
//Area and button position
okButton.frame=CGRect(x:100,y:100,width: 120,height: 120)
//background image
let bkgImage=UIImage(named: "maru.png ")
okButton.setBackgroundImage(bkgImage, for: .normal)
view.addSubview(okButton)
}
}
First, store an instance of UIButton in okButton. Then, determine the position and size of the button with the CGRect type. Load the background image as UIImage type and pass the value to the argument of setBackgroundImage method.
Finally, pass an okButton as an argument to the view.addSubView
method.
This is the method for displaying a button in view.
Recommended Posts