Introduction to Swift Development: button (UIButton)

Source: Internet
Author: User

Reprint please declare source: http://blog.csdn.net/jinnchang/article/details/44403537

1. UIButton Overview

Inheritance: UIButton, Uicontrol, UIView

Control style:

2. Initialization of UIButton(1) using the Buttonwithtype Build button, there are six types of the following:
Enum Uibuttontype:int {case    custom//custom style case    system//Rounded rectangle case    detaildisclosure//Blue small arrow case    Infol ight//Bright exclamation case    infodark//Dark exclamation case    contactadd//cross plus}

(2) Using the Frame custom button

3. Examples of Use
Override Func Viewdidload () {///1, use the existing Type Build button let Commonbutton = Uibutton.buttonwithtype (.  System) as UIButton//Modify button position and size Commonbutton.frame = CGRectMake (SELF.VIEW.FRAME.WIDTH/2-100, 100, 200, 200)// Set button background picture Commonbutton.setbackgroundimage (UIImage (named: "Logo.jpg"), ForState:UIControlState.Normal)//Add Click event C Ommonbutton.addtarget (Self, Action: "Buttonactions:", forControlEvents:UIControlEvents.TouchUpInside)//Set button label com Monbutton.tag = 1//2, custom button Let CustomButton = UIButton (Frame:cgrectmake (SELF.VIEW.FRAME.WIDTH/2-100, 400, 2 00, 200))//Set button caption Custombutton.settitle ("Custom", ForState:UIControlState.Normal)//Set button caption color CustomButton. Settitlecolor (Uicolor.redcolor (), forState:UIControlState.Normal)//Set Button caption Shadow Custombutton.settitleshadowcolor (UIC Olor.blackcolor (), forState:UIControlState.Normal)//Set button Shadow Custombutton.titlelabel?. Shadowoffset = Cgsizemake (1.0, 1.0)//Set button caption font style Custombutton.titlelabel!. Font = uifont.systemfontofsize (18)//Set button title wrap mode custombutton.titlelabel!. Linebreakmode =. Bytruncatingtail//Set button background color Custombutton.backgroundcolor = Uicolor (red:0.8,green:0.8,blue:0.8,alpha:1.0)//Set button inside Part content Margin Custombutton.contentedgeinsets = uiedgeinsetsmake (-100, 0, 0, 0)//Remove the image color burn in the highlighted state Custombutton.adjustsimag    ewhenhighlighted = false;    Remove Image Color Burn custombutton.adjustsimagewhendisabled = false in disabled state;    Add a button to press the Glow effect custombutton.showstouchwhenhighlighted = true; Add Click event Custombutton.addtarget (self,action: "Buttonactions:", forControlEvents:UIControlEvents.TouchUpInside)// Set Button Label Custombutton.tag = 2 Self.view.addSubview (Commonbutton) Self.view.addSubview (CustomButton)}///response button click Thing Piece func buttonactions (sender:uibutton!) {println (Sender.tag)}
4, ForstateThe function of this parameter is to define the state in which the text or picture of the button will appear. There are several states:
    • normal (normal state)
    • highlighted ( pressed state)
    • disabled ( disabled state)
    • Selected ( selected (finger has left)
    • application ( Span style= "White-space:pre" > application flags)
    • reserved ( reserved state)
5. The difference between two ways to set the background image
    • Setbackgroudimage: Picture will be stretched
    • SetImage: Picture remains the original size
6. ConclusionProject on Github address: Uibuttonsample Article last updated: March 18, 2015 10:11:03. the resources are as follows:
(1) UIButton Class Reference
(2) UIKit User Interface catalog:buttons

Introduction to Swift Development: button (UIButton)

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.