Buttons in iOS

In iOS application development one element is very common that is Button, which is used for generating actions or taking some actions. In iOS for creating buttons provides UIButton class.

There are a number of different built-in button types available, like:
-          Add Contact
-          Detail Disclosure
-          Info Dark
-          Info Light
-          System (with custom font size, color and shadow)
-          Custom (with image)


Setting Button Text

Now, we will see how to set the text on a UIButton. UIButton’s have a number of different states and we can set the button text once for all states and also we can assign different values for each state.

For setting the ‘Click on Button’ text on a button, call SetTitle method for UIControlState.Normal and that text will be used for all states:

buttonObj = UIButton.FromType(UIButtonType.RoundedRect);

buttonObj.SetTitle ("Click on Button", UIControlState.Normal);

We can also make each state different like these three states:

buttonObj = UIButton.FromType(UIButtonType.RoundedRect);

This is the normal button that we use mostly:

buttonObj.SetTitle("Normal Button", UIControlState.Normal);

To set button as highlighted use this code:

buttonObj.SetTitle("Highlighted Button", UIControlState.Highlighted);

To disable the button use this code:

buttonObj.SetTitle("Disabled Button", UIControlState.Disabled);

To set the Title button text color light gray:

buttonObj.SetTitleColor(UIColor.LightGray, UIControlState.Disabled);

We can also set these different states using ‘Type’ in ‘Attribute Inspector’  in Xcode by choosing each one from the drop-down-list of the properties panel.


Next, we will learn how to handle button clicks : Handling Button Clicks Events

Last updated:3/31/2019 6:54:24 PM


Leave Comment