iOS UI Controls

iOS: Button

It’s a control that lets the user communicate with the program. It serves as the catalyst for user-performed events. In reaction to user inputs, it runs the customized code.

				
					class UIButton : UIControl  
				
			

One of the most crucial components of iOS applications are the buttons. When a user interacts with a button, certain actions are connected to that button. The iOS application can have the button added programmatically or through the interface builder.

The button is added to the application, and then the following actions are taken.

1.Locate the button by searching through the object collection, then drag the outcome onto the storyboard.

2.When the button is created, provide its kind.

3.Configure the button’s title string or picture.

4.Determine the button’s dimensions based on its contents.

5.Establish the button’s limits to control its size and placement on various device sizes.

Example

In order to enable the button to respond to touch events, let’s build a very basic example in which we add a button to our project and define its action method in the View Controller class file.

Main.storyboard

In this example, we will add a button to the storyboard and use the attribute inspector’s attributes to give it a background color, font size, and title label.

iOS: Button -

Action Connection

We must attach an action of the button object in the ViewController class file in order to execute any action on the button tap. In this project, the button touch up inside event will alter the main view’s backdrop color.

iOS: Button -

ViewController.class

				
					import UIKit  
class ViewController: UIViewController {  
    @IBOutlet var mainView: UIView!  
    var didTap = true  
      
    override func viewDidLoad() {  
        super.viewDidLoad()  
        // Do any additional setup after loading the view.  
    }  
  
    @IBAction func changeBtnTapped(_ sender: Any) {  
        if(didTap)  
        {  
            mainView.backgroundColor = .orange  
            didTap = false  
        }  
        else{  
            mainView.backgroundColor = .groupTableViewBackground  
            didTap = true  
        }  
    }      
}  
				
			

Output

iOS: Button -

Configuring Button Appearance

The look and behavior of a button are determined by its kind. The storyboard file or the init(type:) function can be used to define the button type. There are two varieties of buttons: Custom and System.

Button States

Five states are possible for buttons.

Default

Until the user interacts with the button, it stays in its default state after it is first added to the UIView. As the user plays with the button, the status shifts to different values.

Highlighted

The button changes to the highlighted state when the user taps it.

focused

When the user’s focus is applied to the button, it becomes focused. It is possible to alter the focused state button’s appearance so that it looks different from the selected or focused state.

Selected

The button’s look or functionality are unaffected by this status. Nonetheless, other controls, like as the UISegmentedControl class, can alter their appearance by using this state. We may use the isSelected property to acquire and set this value.

Disabled

When we don’t want the user to engage with the button, we might need to disable it. The isEnabled property can be used to set and retrieve this status.

Content

The button’s content tells the user how the button will behave. In iOS applications, a button’s content can be indicated by its title label text or by a background image. In order to control the button’s content, we might need to set up the UILabel and UIImageView classes.

Using the button object’s titleLabel or imageView properties, we may retrieve the button’s content.

iOS: Button -

Interface Builder Attributes

SNAttributeDescription
1TypeIt represents the button type, which cannot be changed at runtime. It can only be set at the time of button creation. It is accessed by using buttonType property.
2State ConfigIt is the state selector of the button. It defines the state of the button so that the changes can be applied to that state only.
3TitleIt is the title of the button, which can be a plain string or an attributed string.
4Tint, font, and attributeThese attributes are applied to the button title string like tint color, font, text color, shadow color, etc.
5ImageIt is the button’s foreground image.
6backgroundIt is the button’s background image. It is displayed behind the title and the foreground image.

Appearance Attributes

SNAttributeDescription
1Shadow OffsetIt is the shadow offset applied to the button title strings. This attribute can be set at runtime by using shadowOffset property on the titleLabel of the button.
2DrawingIt represents the drawing behavior of the button. In interface builder, we can set three options i.e., showTouchWhenHighlighted, adjustImageWhenHighlighted, and adjustImageWhenDisabled.
3Line BreakIt is the line break mode of the button title label.

Edge Inset Attributes

SNAttributeDescription
1EdgeIt is the edge insets to configure. We can set the separate edge insets to the overall content of the button.
2InsetIt represents the inset values. These values can be accessed by using contentEdgeInsets, titleEdgeInsets, and imageEdgeInsets property.
Share this Doc

iOS: Button

Or copy link

Explore Topic