Usage and partial parameter interpretation of UIButton controls in iOS

Source: Internet
Author: User
Tags set background

UIButton is an extremely common type of control in UI controls, and its class object creation differs from most UI controls using instance method init creation, which is typically created using class methods:

+ (ID) Buttonwithtype: (Uibuttontype) ButtonType;

If you use an instance method to create a UIButton object, such as:

UIButton *button = [[UIButton alloc]initwithframe:cgrectmake (100, 300, 100, 50)];

Object creation is not a problem, but when you set a caption for the button object, such as:

[Button settitle:@ "ASD" forstate:uicontrolstatenormal];

But the program is not displayed on the screen after the run, but this is not a bug, but when the button is created using an instance method, the default color of the caption text is white, it is invisible on the white screen or on the screen without setting the background color. If you use a class method to create a UIButton object, you need to enter a parameter of type Uibuttontype, which is an enumeration, as follows:

typedef ns_enum (Nsinteger, Uibuttontype) {

Uibuttontypecustom = 0,//custom type, i.e. no type, when selecting this parameter city is the same as using the class method

Uibuttontypesystem,//standard type, general selection, starting with the iOS7.0 version

Uibuttontypedetaildisclosure,//A blue information flag compared to the standard type

Uibuttontypeinfolight,//information's logo is a little brighter.

Uibuttontypeinfodark,//information's logo is darker.

Uibuttontypecontactadd,//One plus sign compared to the standard type

Uibuttontyperoundedrect = Uibuttontypesystem,//deprecated, replaced by standard type

};

The default color for enumeration types other than Uibuttontypecustom is blue, and when you place a picture on a button, the original effect is blue.

The custom type can correctly display the color of the picture. In addition, if you set both the picture and the text for the button, the picture is in the left text on the right.

Set caption text:-(void) Settitle: (NSString *) title forstate: (uicontrolstate) state;

Set Picture:-(void) SetImage: (UIImage *) image forstate: (uicontrolstate) state;

The function is to display the string object under the status State, title, image, where the picture is the same size if you want to display different pictures in different states, otherwise it will be displayed incorrectly.

Where Uicontrolstate is the enumeration type, as follows:

typedef ns_options (Nsuinteger, uicontrolstate) {

UIControlStateNormal = 0,//normal state

uicontrolstatehighlighted = 1 << 0,//Highlight state, when button is clicked from normal state to highlight state, release automatically converted to normal turn state

uicontrolstatedisabled = 1 << 1,//unavailable state, via property enabled settings

uicontrolstateselected = 1 << 2,//Select Transit State, set by property selected

Uicontrolstateapplication = 0x00ff0000,//When application flag

uicontrolstatereserved = 0xff000000//reserved for internal frames

};

The common usage is:

[Button Settitle:title1 Forstate:uicontrolstatenormal];

[Button Settitle:title2 forstate:uicontrolstatehighlighted];

The normal state of the button displays Title1, Title2 is displayed when pressed, and Title1 is displayed when released.

Set the picture similarly.

Set background Image:-(void) SetBackgroundImage: (UIImage *) image forstate: (uicontrolstate) state

Unlike setting up a picture, the background picture appears below the text, not the left.

Set caption text color:-(void) Settitlecolor: (Uicolor *) color forstate: (uicontrolstate) state

This method only changes the title text color, does not affect the picture color, and the property Tintcolor affects both the title text and the picture color, but does not affect the background image color. Also, when UIButton is created, the property Tintcolor is not valid when you select Uibuttontypecustom or use instance method creation.

Set Response event:-(void) AddTarget: (ID) Target action: (SEL) Action forcontrolevents: (uicontrolevents) controlevents;

This means that when controlevents occurs, target invokes the action method, which, by OC, sends an action message to the target object.

Where ControlEvents is the enumeration type, as follows:

typedef ns_options (Nsuinteger, uicontrolevents) {

Uicontroleventtouchdown = 1 << 0,//button press Event

Uicontroleventtouchdownrepeat = 1 << 1,//double-tap or multiple-click event

Uicontroleventtouchdraginside = 1 << 2,//Control window Internal drag event

Uicontroleventtouchdragoutside = 1 << 3,//Control window external drag event

Uicontroleventtouchdragenter = 1 << 4,//drag from outside the control window into internal events

Uicontroleventtouchdragexit = 1 << 5,//control window outside drag into internal event

UIControlEventTouchUpInside = 1 << 6,//Control window internal Click to lift the event that occurs after the Uicontroleventtouchdown event

Uicontroleventtouchupoutside = 1 << 7,//Within the control window, click outside the window to lift the event

Uicontroleventtouchcancel = 1 << 8,//All click Cancel events, such as: Too many fingers, locked, or have phone access

uicontroleventvaluechanged = 1 << 12,//Control value Change event, more for Uislider,uisegmentcontrol,uipagecontrol

Uicontroleventeditingdidbegin = 1 <<,//Uitextfield start editing events, that is, when you enter the first response

uicontroleventeditingchanged = 1 <<, when the text in the//uitextfield changes

Uicontroleventeditingdidend = 1 <<//uitextfield When the first response is canceled when the edit is finished

Uicontroleventeditingdidendonexit = 1 << 19,//Click ' Return key ', the default is the line-break key, will automatically cancel the first response to end the edit

Uicontroleventalltouchevents = 0X00000FFF,//All touch events

Uicontroleventalleditingevents = 0x000f0000,//all Uitextfield edit events

uicontroleventapplicationreserved = 0x0f000000,//range available for application use

uicontroleventsystemreserved = 0xf0000000,//range reserved for internal framework use

Uicontroleventallevents = 0xFFFFFFFF//All Events

};

Cancel Response event:-(void) Removetarget: (ID) Target action: (SEL) Action forcontrolevents: (uicontrolevents) controlevents;

The effect is to cancel the target response to the action when the controlevents occurs, that is, when the event occurs, target no longer triggers the method action, but only if the method response to the event has been added before, where the parameters can be set to nil, indicating any, all, That is, remove all.

Usage and partial parameter interpretation of UIButton controls in iOS

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.