How to create UIButton programmatically ?

Ever wondered How to create UIButton programmatically ? Follow few simple steps:-

1) Create a New project select – Single View Application.

Screen Shot 2016-02-25 at 9.37.06 PM

2) Add a suitable name to your application and set the organisation Identifier to com.YourCompanyname.myapp.

Screen Shot 2016-02-28 at 9.12.59 AM

3)Now create an instance of UIButton and assign frame to it specifying its x coordinate, y coordinate, width and height.

 

Screen Shot 2016-02-25 at 9.50.45 PM

4) TO add border and set rounded Corners of your button

 

Now, your viewDidLoad will look as:-

Screen Shot 2016-02-25 at 9.57.44 PM

5) To set title of button and background Color.

 

Screen Shot 2016-02-28 at 8.52.39 AM

6) Finally to add action on button and place it on view.

 

 

Screen Shot 2016-02-28 at 8.57.15 AM

7) Create a method for your button action and run the app.

Screen Shot 2016-02-28 at 9.10.01 AM

 

That a simple explanation How to create UIButton programmatically. I’ll be back soon with something more usefull in my next Tutorial.

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.