iOS Development UI Basics UIButton

Source: Internet
Author: User

What is a button?

There are a series of buttons, generally we do the project when the criteria for judging the button is: When the click can respond to the general is the button

Buttons can be more powerful: they can display text and display pictures , and we can customize the buttons to adjust the position and arrangement of pictures and text.

We generally create a button to drag a button directly inside the storyboard actually storyboard the essence or convert some of the properties we set into codeThe state of UIButton

    • Normal state (normal)
    1. Defaults (default)
    2. The corresponding enumeration constant is UIControlStateNormal
    • Highlight status (highlighted)
    1. When the button is pressed down, the finger is not released.
    2. The corresponding enumeration constant uicontrolstatehighlighted
    • Unavailable status (disabled)
    1. If the Enabled property of the button is set to No or is in an unavailable (disable) state, the button cannot be clicked
    2. The corresponding enumeration constant uicontrolstatedisabled
    • Checked status (selected)
    1. Status displayed when the button is selected
    2. The corresponding enumeration constant uicontrolstateselected
Set the background picture of the button
    • Set the button to get a background picture in different states note: In order to ensure that the picture in the highlighted state of the normal display must be set to the type of the button custom
button style using the Code creation button

Types of UIButton
    • Uibuttontypecustom//Custom type
    • Uibuttontyperoundedrect//Fillet type
    • Uibuttontypedetaildisclosure//Detail Display button
    • Uibuttontypeinfolight//Light Background Info button
    • Uibuttontypeinfodark//Dark Background information button
    • Uibuttontypecontactadd//Add button

UIButton Common Properties

//set the caption content for the corresponding state- (void) Settitle: (NSString *) title forstate: (uicontrolstate) state;//indicates the state of the button this has been described above//set the caption color for the corresponding state- (void) Settitlecolor: (Uicolor *) Color forstate: (uicontrolstate) state; //Sets the caption shadow color for the corresponding state- (void) Settitleshadowcolor: (Uicolor *) Color forstate: (uicontrolstate) state; //Picture of the button that sets the corresponding State- (void) SetImage: (UIImage *) Image forstate: (uicontrolstate) state; //set the button background picture for the corresponding state- (void) SetBackgroundImage: (UIImage *) image forstate: (uicontrolstate) state;

Attention:

When you set text to a button, the Apple document describes that you cannot use a Label object to set the color or shadow color of the text, instead you must use SetTitleColor:forState:and settitleshadowcolor:forstate: These two methods can modify these values.

Add an event to a button

Add an event for a button in the following ways

Add Event -(void) AddTarget: (ID) target action: (SEL) Action forcontrolevents: ( uicontrolevents) controlevents; These events are touch-based, value-based, edit-based. The following events are triggered. At the click of the button is the button is concave down and then bounces up to trigger the event,

There are several event states for the button:

    • Uicontroleventtouchdown//Press
    • Uicontroleventtouchdownrepeat multiple Presses
    • UIControlEventTouchUpInside//Loosen in button and certain periphery
    • Uicontroleventtouchupoutside//button outside release

Other properties

adjustsimagewhendisabled when the button is disabled, the color of the image is drawn darker and the default is yes.

adjustsimagewhenhighlighted when the button is highlighted, the color of the image is drawn darker and the default is yes.

showstouchwhenhighlighted if the flash effect is set to Yes, the foreground image obscures the middle part;

Contentedgeinsets sets the internal contents of the button (including the button picture and caption) from the top and bottom of the button edge.

Speaking of Contentedgeinset have to say the following two properties:

Difficult issues

Sometimes we want UIButton's title to align to the left, we set it up? btn.textLabel.textAlignment = Uitextalignmentleft is not useful

We need to set btn.contenthorizontalalignment = Uicontrolcontenthorizonalignmentleft;

But the problem comes out again, when the text will close to the left border, we can set btn.contentedgeinsets = Uiedgeinsetsmake (0,10, 0, 0); Keeps the text distance from the left border by 10 pixels.

All right, I'll share it here today. If you have a better suggestion, please leave a comment! Thank you for your support

iOS Development UI Basics UIButton

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.