IOS development-UIButton (button) of the UIKit control and uikituibutton

Source: Internet
Author: User
Tags uicontrol

IOS development-UIButton (button) of the UIKit control and uikituibutton

(Note: This article describes some of the method attributes used in my daily development as a memo)

Like UIView, UIButton is the most commonly used and common UIKit control in iOS development.

UIButton inherits from UIControl, while UIControl inherits from UIView. Therefore, UIButton is also a subclass of UIView.

In any iOS app, UIButton is everywhere and can interact with users and pass events!

Create UIButton

1 // instantiate (create) button 2/** 3 * Type value: 4*1. UIButtonTypeContactAdd: + button 5*2. UIButtonTypeCustom: Custom button 6*3. UIButtonTypeDetailDisclosure: 'I' button 7*4. UIButtonTypeInfoDark: 'I' button 8*5. UIButtonTypeInfoLight: 'I' button 9*6. UIButtonTypeRoundedRect: the rounded corner button (this final effect is not set now) 10*7. UIButtonTypeSystem: the effect is the same as the above RoundedRect 11 */12 UIButton * button = [UIButton buttonWithType: UIButtonTypeSystem]; 13 // set the background color 14 button. backgroundColor = [UIColor lightGrayColor]; 15 // set the region range to 16 buttons. frame = CGRectMake (110,234,100,100); 17 // Add to View 18 [self. view addSubview: button];

Attribute

 

1 // set whether to select status 2 button. selected = NO; 3 // set whether to receive the touch event 4 button. enabled = YES; 5 // set the tag value to 6 buttons. tag = 0; 7 // set the font size of the button title text to 8 buttons. titleLabel. font = [UIFont systemFontOfSize: 18366f]; 9 // by setting this attribute, you can adjust the text of the button to display 10 // NSLineBreakByCharWrapping in the area of the button, same effect 11 button. lineBreakMode = NSLineBreakByWordWrapping; 12 // set the rounded corner diameter 13 button. layer. cornerRadius = 10.0f; 14 // cut the part exceeding 15 buttons. layer. masksToBounds = YES; 16 // Border Width 17 button. layer. borderWidth = 1.0f; 18 // border color 19 button. layer. borderColor = [UIColor purpleColor]. CGColor;

 

Method

1 // set the button status title text 2/** 3 * State value: 4*1. UIControlStateNormal: normal state (default state) 5*2. UIControlStateSelected: The selected status is 6*3. UIControlStateHighlighted: highlighted 7*4. UIControlStateDisabled: disabled status 8 * the four 9 */10 [button setTitle: @ "button" forState: UIControlStateNormal] are usually used by developers. 11 // set the button status icon 12 // here the value of state is the same as above 13 [button setImage: [UIImage imageNamed: @ "01.png"] forState: UIControlStateNormal]; 14 // set the button status background image 15 // The value of state here is the same as that above 16 [button setBackgroundImage: [UIImage imageNamed: @ "02.png"] forState: UIControlStateNormal]; 17 // Add (BIND) Click Event 18/** 19 * parameter description: 20 * target: event listening object. Generally, self21 * action: callback event method, only one parameter 22 * ControlEvents can be uploaded for the button itself: Listener type 23 * UIControlEventTouchUpInside: Click 24 * UIControlEventTouchCancel: Touch to cancel 25 * UIControlEventTouchDown: Touch to finish 26 * so far, most of the tasks I use are click events, which are rarely used to cancel and complete 27 */28 [button addTarget: self action: @ selector (ButtonTouchEvent :) forControlEvents: UIControlEventTouchUpInside];

Event

-(Void) ButtonTouchEvent :( UIButton *) sender {// click the Event Callback processing button to select the sender. selected =! Sender. selected ;}

 

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.