IOS Study Notes (7)-UI basics UIButton

Source: Internet
Author: User

UIWindow and UIViewController are written earlier. They are all some frameworks. The framework must be filled with specific views to form our applications. In mobile application development, the UI occupies a large part, the most basic UI implementation is to use various controls provided by the system. Others are custom implementations. The author is currently in the entry state and can only write basic controls.

IOS provides basic UI controls such as UIButton, UILable, UITextField, and UIImageView, which are inherited from UIView. Here we will first use UIButton as a trainer. Why do we use UIButton? Because UIbutton inherits from UIControl and UIControl is derived from the UIView class, each control has many view features, including the ability to attach to other views, all controls have a set of common attributes and methods, including display content, click events, and so on. The subclass of UIControl has the ability to process events.

Figure, UIControl


UIButton definition:

UIButton can be created using initWithFrame or buttonWithType:

1) initWithFrame

  UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(60, 60, 200, 60)];    btn.backgroundColor=[UIColor greenColor];    [btn setTitle:@"btn1" forState:UIControlStateNormal];    [self.view addSubview:btn];

2) buttonWithType

UIButton *btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];    CGRect rect=CGRectMake(60, 160, 200, 60);    btn2.frame=rect;    btn2.tag=1001;    btn2.backgroundColor=[UIColor colorWithRed:30/255.0 green:200/255.0 blue:125/255.0 alpha:1.0];    [btn2 setTitle:@"btn2" forState:UIControlStateNormal];    [btn2 addTarget:self action:@selector(btn2Pressed) forControlEvents:UIControlEventTouchDown];    [self.view addSubview:btn2];

Btn2Pressed method:

-(void)btn2Pressed{    NSLog(@"button pressed");}

UIButtonType:

Typedef enum {UIButtonTypeCustom = 0, // no button type custom, no style UIButtonTypeRoundedRect, // rounded rect, flat white button, like in address card white rounded rectangle, similar to preference settings, the table unit or address book card UIButtonTypeDetailDisclosure, // The Blue disclosure button can be placed next to any text, and the tiny circle information button used by the UIButtonTypeInfoLight, // widget, you can put UIButtonTypeInfoDark next to any text, // The UIButtonTypeContactAdd button for the dark circle information used in the white background, // The Blue plus sign (+) button, which can be placed next to any text} UIButtonType;

Common UIButton attributes:

// Set the title content of the corresponding status. default is nil. title is assumed to be single line

-(Void) setTitle :( NSString *) title forState :( UIControlState) state;

// Set the title Color of the corresponding status

-(Void) setTitleColor :( UIColor *) color forState :( UIControlState) state;

// Set the title shadow color of the corresponding status

-(Void) setTitleShadowColor :( UIColor *) color forState :( UIControlState) state;

// Set the icon image in the corresponding status

-(Void) setimage :( uiimage *) image forstate :( uicontrolstate) State;

// Set the button background image in the corresponding status

-(Void) setbackgroundimage :( uiimage *) image forstate :( uicontrolstate) State;


Uibutton's uicontrolstate:

typedef NS_OPTIONS(NSUInteger, UIControlState) {    UIControlStateNormal       = 0,    UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set    UIControlStateDisabled     = 1 << 1,    UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)    UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use    UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use};

For more attributes, see the official documentation.

UIButton add events: UIButton uses the following method to add events.

[btn addTarget:<#(id)#> action:<#(SEL)#> forControlEvents:<#(UIControlEvents)#>]

These events are based on touch, value, and editing. The following events are supported.

typedef NS_OPTIONS(NSUInteger, UIControlEvents) {    UIControlEventTouchDown           = 1 <<  0,      // on all touch downs    UIControlEventTouchDownRepeat     = 1 <<  1,      // on multiple touchdowns (tap count > 1)    UIControlEventTouchDragInside     = 1 <<  2,    UIControlEventTouchDragOutside    = 1 <<  3,    UIControlEventTouchDragEnter      = 1 <<  4,    UIControlEventTouchDragExit       = 1 <<  5,    UIControlEventTouchUpInside       = 1 <<  6,    UIControlEventTouchUpOutside      = 1 <<  7,    UIControlEventTouchCancel         = 1 <<  8,    UIControlEventValueChanged        = 1 << 12,     // sliders, etc.    UIControlEventEditingDidBegin     = 1 << 16,     // UITextField    UIControlEventEditingChanged      = 1 << 17,    UIControlEventEditingDidEnd       = 1 << 18,    UIControlEventEditingDidEndOnExit = 1 << 19,     // 'return key' ending editing    UIControlEventAllTouchEvents      = 0x00000FFF,  // for touch events    UIControlEventAllEditingEvents    = 0x000F0000,  // for UITextField    UIControlEventApplicationReserved = 0x0F000000,  // range available for application use    UIControlEventSystemReserved      = 0xF0000000,  // range reserved for internal framework use    UIControlEventAllEvents           = 0xFFFFFFFF};


/**

* @ Author Zhang xingye * http://blog.csdn.net/xyz_lmn* IOS entry group: 83702688

* Android Development Group: 241395671

* My Sina Weibo:@ Zhang xingye tbow*/

Refer:

Http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html

Http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKit_Framework/Introduction/Introduction.html#//apple_ref/doc/uid/TP40006955-CH1-SW1

I. Structure of uikit

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.