IOS UIButton Common Settings

Source: Internet
Author: User

Class Method Buttonwithtype

UIButton *btn= [UIButton buttonwithtype:uibuttontyperoundedrect];

Style has the following

typedef enum {      Uibuttontypecustom = 0,//custom, no style    uibuttontyperoundedrect,//white rounded rectangle, like preference table cell or Address Book Card    uibuttontypedetaildisclosure,//Blue Disclosure button, can be placed next to any text    uibuttontypeinfolight,//widget (widget) Use small circle information button, can be placed next to any text    uibuttontypeinfodark,//Dark Circle Info button    used on white background uibuttontypecontactadd,//Blue Plus (+) button, can be placed next to any text} Uibuttontype;

Setting Properties

Frame Property

Section 2 method to create a button you can give the button Frame property to assign a value, using a CGRect structure to set his position and size

CGRect btnframe = CGRectMake (10.0, 10.0, 60.0, 44.0);

Btn.frame =btnframe;

Property

for any button in a particular state, you can set the button caption for the button in that state. You can set it with the settitle method :

[Btn1 settitle:@ "BTN1" forstate:uicontrolstatenormal];

You can also set a graph for a certain state of the button. You can use SetImage

[Btn2 setimage:[uiimage imagenamed:@ "pic"] forstate:uicontrolstatenormal];

In addition, you can set the color and shadow of the caption for each button state, as well as the background of the button.

Method Settitlecolor and the Settitleshadowcolor all Need a Uicolor object to do the argument

[btn Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];//set title color  [BTN1 settitleshadowcolor:[ Uicolor Graycolor] Forstate:uicontrolstatenormal];//shadow  [btn1 setbackgroundimage:[uiimage imageNamed:@ "PIC"] forstate:uicontrolstatehighlighted];//background image
Several of the methods mentioned above are Common Parametersforstate.This parameter determines the state in which the title, image, or other property will appear. You can programmatically make the button change in that State

enum {      UIControlStateNormal       = 0,//normal                           uicontrolstatehighlighted  = 1 << 0,//Highlight      uicontrolstatedisabled     = 1 << 1,//disabled      uicontrolstateselected     = 1 << 2,//selected      Uicontrolstateapplication  = 0x00ff0000,//When application flags are used      uicontrolstatereserved     = 0xff000000//reserved for internal frames  };  

Just master the first four states.

When the button is highlighted or disabled, theUIButton class can adjust its appearance, and the following properties allow you to fine-tune the appearance of the button as needed:

adjustsimagewhenhighlighted

By default, when the button is disabled, the image is painted darker. To disable this feature, set this property to NO:

btn1.adjustsimagewhenhighlighted = NO;

Adjustsimagewhendisabled

By default, when the button is disabled, the image will be painted in a lighter color. To disable this feature, set this property to NO:

btn1.adjustsimagewhendisabled = NO;

showstouchwhenhighlighted

this property is set to YES to make the button glow when pressed. This can be used for informational buttons or some important buttons:

btn1.showstouchwhenhighlighted = YES;


Display controls

[Self.view ADDSUBVIEW:BTN1]; [Self.view ADDSUBVIEW:BTN2];


overriding Paint behavior

You can customize your own button class by using the Sub-type button. In subclasses you can overload the following methods, which return the cgrect structure, indicating the bounds of each component of the button.

Note: Do not call these methods directly , these methods are what you write to the system call.

<pre name= "code" class= "OBJC" >backgroundrectforbounds//Specify background bounds  contentrectforbounds//Specify content boundaries  titlerectforcontentrect//Specify text caption bounds  imagerectforcontentrect//Specify button image boundary  //Example:-(CGRect) Imagerectforcontentrect: (cgrect) bounds{  return CGRectMake (0.0, 0.0, 44, 44);}


Add action

What's the button for? Used to excite an action or event. Then we're going to add an action for him, as uicontrol says :

[Btn addtarget:self Action: @selector (btnpressed:) forcontrolevents:uicontroleventtouchupinside];-(void) btnpressed :(id) sender{uibutton* btn = (uibutton*) sender;

IOS UIButton Common Settings

Related Article

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.