【 例 】 In order to avoid a quarrel, advance declaration: this article is pure translation, only for learning, plus the level is limited, forgive me!

UIButton

Control components that control the execution of your custom code in response to user interactions.

An overview of the

When you click on a button or select a button that gets focus, it performs any actions that are bound to it. You use text labels, images, or both to communicate the purpose of the button. The look of the button is configurable, so you can color the button or format the title to match the design of the application. You can add buttons to the interface using code or IB.

  1. Set the type when you create the button.
  2. Provide a title string or image; Set the appropriate size for the button.
  3. Attach one or more actions to a button.
  4. Set automatic layout rules to control the size and position of buttons on the interface.
  5. Provide accessibility information and localization strings.

Response button click

When a user clicks a button, the button notifies your application in target-Action design mode. Assign an action method to the button and specify which events trigger your method call, rather than holding touch events directly. At runtime, the button handles all incoming touch events and calls your methods in response.

You can use the addTarget: action: forControlEvents: method or IB to link button and your action method. The action method signature takes one of the three forms listed in Listing1. Select the form that requires information to be provided in response to a button click.

Listing 1

- (IBAction)doSomething;
- (IBAction)doSomething:(id)sender;
- (IBAction)doSomething:(id)sender forEvent:(UIEvent*)event;
Copy the code

Configuration button Appearance

The type of a button defines its basic appearance and behavior. You can use the buttonWithType: method or the storyboard to specify the type of the button when it is created. Once a button is created, you can’t change its type. The most common button types are the Custom and System types, but other types can be used when appropriate.

Note If you want to configure the appearance for all buttons in your application, you need to use the appearance proxy object. The UIButton class implements the appearance class method, which you can use to get the appearance proxy for all the buttons in your application.

Button state

The button has five states that define the look: default, highlighted, focused, selected, and disabled. When you add a button to the interface, it is in default state, which means the button is available and the user is not interacting with it. When the user interacts with the button, his state changes to other values. For example, when the user clicks on a button with a caption, the button becomes highlighted.

When configuring buttons programmatically or IB, you can specify properties for each state individually. In IB, use the property inspector State Config Control to select the appropriate State and configure other properties. If you don’t set a property for a particular state, the UIButton class provides a reasonable default behavior for it. For example, an unavailable button is usually dark gray and does not appear bright when clicked. This class of other properties, such as adjustsImageWhenHighlighted and adjustsImageWhenDisabled attributes, allows you to change the default behavior in some special circumstances.

content

The content of the button contains a title string or image that you specify. The content you specify is used to configure UILabel and UIImageView objects managed by the button itself. You can retrieve these objects using the titleLabel and imageView properties and modify their values directly. The methods of this class also provide shortcuts for configuring the appearance of strings and images.

In general, you can configure buttons with titles or images and resize them accordingly. Buttons can also have a background image behind the content you specify. You can set a title and an image for the button at the same time, and it should look like figure 2. You can use explicit properties to retrieve the current content of the button.

When setting the button content, you must set the title, image, and appearance properties for each state. If you do not customize the look and feel for a particular state, the button will use the values associated with the default state and add any customizations that are appropriate. In the highlighted state, for example, the image-based button draws a highlight above the default image if no custom image is provided.

The base of color

You can use tintColor to specify a background color for custom buttons. This property sets the appearance level for the button’s image or text. If you do not set the background color, the button will use the background color of the parent view.

Concave boundary

You can add or remove space around content using indented custom or system buttons. You can set button titles (title geinsets), indented values for images (Image geinsets), or both title and image contentEdgeInsets. When applied, indented affects the corresponding content rectangle of the button, which is used by the auto layout engine to determine the position of the button.

Info, Contact, Disclosure and other buttons cannot be adjusted for concave boundaries.

The IB properties

Table 1 lists the core attributes for configuring buttons in IB

Attribute Description
type Button type. This property determines the default Settings for many button properties. The value of this property cannot be changed at run time, but you can use itbuttonTypeProperty to get the type value
State configuration State selector. After selecting a value in the controller, changes to the button properties are applied to the specified state
The title The title of the button. You can set the title of the button with a simple string or property string
(Title font and properties) Fonts and other properties applied to button titles. The configuration options depend on whether you set a simple string or a property string for the button. If it’s a simple string, then you can customize the font, text color, and shadow color. For attribute strings, you can specify alignment, text orientation, notch, hyphen, and many other options.
The picture Foreground image of the button. Normally, you would set a template image for the button foreground, but you can set any image in the Xcode project.
background Button background image. The background image is at the bottom of the title and foreground image.

Table 2 lists the attributes that affect the appearance of the button.

Attribute Description
The shadow offset The offset and behavior of the button shadow. Shadows affect only the title string. When the button state changes to highlight or from highlight to another state, enable the Highlight Inversion option to change the shadow highlighting.
painting The drawing behavior of buttons.
A newline Newline option for button text. Use this property to define how to change the button title to fit the available space.

Table 3 lists the edge indented attributes. Use the button edge indented to change the rectangle of the button content.

Attribute Description
The border The edges to be configured are concave. You can set the buttons to indent the edges of all content, title and image separately.
concave Concave values. A positive value Narrows the corresponding boundary, pushing it closer to the center. Negative values expand the boundary, pushing it further away from the center. Available at run timetitleEdgeInsets.imageEdgeInsetsandcontentEdgeInsetsProperty gets these values.

For the IB properties that buttons inherit, see UIControl and UIView.

internationalization

To internationalize a button, you need to set a localization string for the button title text. (You may be able to localize a button image appropriately.)

When building a storyboard interface, use Xcode-based internationalization to configure the localization your project supports. When you add localization, Xcode creates a localized string file. When programmatically configuring your interface, use the system’s built-in support to load localized strings and resources. For more information on interface internationalization, see the Internationalization and Localization guide.

accessibility

The default button is accessible. The default accessibility feature for buttons is to enable button and user interaction.

When VoiceOver is enabled on the device, accessibility tabs, features, and prompts are passed back to the user. The button title overrides its usability tag; Even if you set a custom value to the tag, VoiceOver will tell you the value of the title. Once the user clicks the button, VoiceOver says its information. For example, when the user clicks an optional button on the camera, VoiceOver says something like this:

  • “Options.button. show Additional camera Options.”

For more information about iOS control access, see Accessibility Information in UIControl. For general information about making your interface accessible, see the iOS Accessibility Programming Guide.

topic

Create button

+ buttonWithType:

Method creates and returns a new button of the specified type.

The statement

+ (instancetype)buttonWithType:(UIButtonType)buttonType;
Copy the code

parameter

ButtonType: indicates the buttonType. To see possible values see.

The return value

Newly created button.

discuss

This method is a convenience constructor with a special configuration to create a button object. If you subclass UIButton, the strength that this method returns is not your subclass. If you want to create a specific subclass, you need to create the button directly by calling the alloc/init method.

When creating a custom button whose type is UIButtonTypeCustom, the initial frame of the button is (0, 0, 0, 0). Before adding a button to the interface, you need to update the frame to the appropriate value.

UIButtonType

Enumeration specifies the style of the button.

The statement

iOS

typedef enum UIButtonType : NSInteger {
    UIButtonTypeCustom = 0,
    UIButtonTypeSystem,
    UIButtonTypeDetailDisclosure,
    UIButtonTypeInfoLight,
    UIButtonTypeInfoDark,
    UIButtonTypeContactAdd,
    UIButtonTypeRoundedRect = UIButtonTypeSystem
} UIButtonType;
Copy the code

tvOS

typedef enum UIButtonType : NSInteger {
    UIButtonTypeCustom = 0,
    UIButtonTypeSystem,
    UIButtonTypeDetailDisclosure,
    UIButtonTypeInfoLight,
    UIButtonTypeInfoDark,
    UIButtonTypeContactAdd,
    UIButtonTypePlain,
    UIButtonTypeRoundedRect = UIButtonTypeSystem
} UIButtonType;
Copy the code

constant

**UIButtonTypeCustom: ** No button styles **UIButtonTypeSystem: ** System style buttons, the same as those displayed on navigation and toolbars. UIButtonTypeDetailDisclosure: * * * * button for details. **UIButtonTypeInfoLight: ** Highlight the background information button. **UIButtonTypeInfoDark: ** Message button with dark background. **UIButtonTypeContactAdd: ** Contact add button. **UIButtonTypePlain: ** Standard system button without blurry background view. **UIButtonTypeRoundedRect: ** Rounded rectangle button.

Configuration button Title

titleLabel

Property displays a view of the currentTitle property value for the button

The statement

@property(nonatomic, readonly, strong) UILabel *titleLabel;
Copy the code

discuss

Although this implementation is read-only, its properties are read and write. Use these properties to preliminarily configure button text. Such as:

UIButton *button = [UIButton buttonWithType: UIButtonTypeSystem];
button.titleLabel.font = [UIFont systemFontOfSize:12];
button.titleLabel.lineBreakMode = NSLineBreakTruncatingTail;
Copy the code

Don’t use label objects to set text and shadow colors. Instead, use this class setTitleColor: forState and setTitleShadowColor: forState: methods to set these values. Use setTitle:forState to set the text of the tag (button.titlelabel.text cannot set the text).

Even if the button is not already displayed, the titleLabel property returns a value. The value of this property is nil for system buttons.


– titleForState:

Returns the title associated with the specified state.

The statement

- (NSString *)titleForState:(UIControlState)state;
Copy the code

parameter

*state: * Use the state of the title. The available status values are declared in UIControlState.

The return value

A title for a specific state. If the specified state does not have a title, this method returns a title associated with the UIControlStateNormal state.


– setTitle:forState:

Set a title for a particular state

The statement

- (void)setTitle:(NSString *)title forState:(UIControlState)state;
Copy the code

parameter

*title: * The title used by a particular state *state: * the state used by a particular title, see UIControlState


– attributedTitleForState:

Returns the styled title associated with the specified state.


– setAttributedTitle:forState:

Sets the styled title for the specified state.


– titleColorForState:

Returns the title color used for a particular state.


– setTitleColor:forState:

Sets the title color for a specific state.


– titleShadowColorForState:

Returns the shadow color of a specific state.


– setTitleShadowColor:forState:

Sets the shadow color for a specific state.


reversesTitleShadowWhenHighlighted

Boolean value that determines whether to change the header shadow when the button is highlighted.


Display of configuration button

adjustsImageWhenHighlighted

Boolean value that determines whether to change the image when the button is highlighted.

adjustsImageWhenDisabled

Boolean value that determines whether to change the image when the button is not available.

showsTouchWhenHighlighted

Boolean value that determines whether the button glows when clicked.

– backgroundImageForState:

Returns a background image for a specific state.

– imageForState:

Returns a picture in a specific state.

– setBackgroundImage:forState:

Sets the background image for a specific state.

– setImage:forState:

Sets the image for a specific body.

tintColor

Background color applied to button titles and images.

Configure edge indented

titleEdgeInsets

Set the title border.

imageEdgeInsets

Set image borders.

contentEdgeInsets

Sets the button content border.

Get the current state

buttonWithType

Button type.

currentTitle

Current title.

currentAttributedTitle

Gets the current styled title.

currentTitleColor

Current label color.

currentTitleShadowColor

Current title shadow color.

currentImage

Button to display the current image.

currentBackgroundImage

Button displays the current background image.

imageView

Picture view of the button.

Get the size

– backgroundRectForBounds:

Returns the rectangle in which the receiver draws the background.

– contentRectForBounds:

Returns the rectangle in which the receiver draws the entire content.

– titleRectForContentRect:

Returns the rectangle in which the receiver draws the title.

– imageRectForContentRect:

Returns the rectangle from which the receiver drew the image.


addTarget:action:forControlEvents:

Associate target objects and action methods with space.

The statement

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
Copy the code

parameter

* Target: * Target object, that is, the object on which the action method is called. If you set it to nil, UIKit will look up the response chain for the object and pass that message to the object for the action specified in the response. * Action: * is called the selector identifier of the action method. You can specify a selector that matches any method signature in UIControl. This parameter must be non-null. ControlEvents: * A bitmask used to identify the specific control event that calls the action method. At least one constant is usually specified. For a list of constants, see UIControlEvents