How to customise iOS button to set text and color?


In this post we will be seeing how to customise iOS button.

So let’s get started.

Step 1 − Open Xcode -> New Project -> Single View Application -> Let’s name it “CustomiseButton”

Step 2− Open Main.storyboard and add a button as shown below. We will customize this button

There are two ways to customise this button

  • Using Storyboard
    • Step 1 − Click on the button
    • Step 2 − In the right panel, in the attribute inspector you can change the text color, text and background color of the button as shown below

Run the project you will see the customise button as below

Now we will see the second way to customise the button

  • Programmatically Customise the button
    • Step 1 − Create @IBOutlet for the button, name it customButton.
    • Step 2 − In viewDidLoad method customise the button as follows
override func viewDidLoad() {
   super.viewDidLoad()
   customButton.setTitle("My Custom Button", for: .normal)
   customButton.backgroundColor = UIColor.green
}

Run the project, you see the customised button as shown below

raja
Published on 11-Sep-2019 11:39:47
Advertisements