The UIButton of Swift learning

Source: Internet
Author: User

//

Viewcontroller.swift

button

//

Created by Su on 15/12/7.

Copyright©2015 year Tian. All rights reserved.

//

Import UIKit

Class Viewcontroller:uiviewcontroller {

Override Func Viewdidload () {

Super.viewdidload ()

Create a button

Let Button:uibutton = UIButton (Frame:cgrect (x:110, y:70, width:100, height:50))

Button.settitlecolor (Uicolor.redcolor (), ForState:UIControlState.Normal)

Set the text in a different state, in the normal state and in the pressed state

Button.settitle ("You order me!", ForState:UIControlState.Normal)

Button.settitle ("You really do," forState:UIControlState.Highlighted)

Add an interactive response to the button by using the Addtarget method, in turn, by pressing the event

Button.addtarget (Self, Action: "Press:", ForControlEvents:UIControlEvents.TouchUpInside)

Self.view.addSubview (Button)

Create a Graphics button

Let Normalimage = UIImage (named: "BTN1")

Let Highlightedimage = UIImage (named: "BTN2")

Let button2 = UIButton (Frame:cgrect (x:110, y:200, width:100, height:30))

Button2.setimage (Normalimage, ForState:UIControlState.Normal)

Button2.setimage (Highlightedimage, forState:UIControlState.Highlighted)

Self.view.addSubview (Button2)

Create a picture-plus-text button

Let Button3 = UIButton (Frame:cgrect (x:0, y:250, width:400, height:30))

Button3.setimage (UIImage (named: "Btn1"), ForState:UIControlState.Normal)

Button3.titlelabel?. Font = uifont.systemfontofsize (14)

Button3.imageview?. Contentmode = Uiviewcontentmode.scaleaspectfit

Button3.settitlecolor (Uicolor.blackcolor (), ForState:UIControlState.Normal)

Button3.settitle ("FSDFDSFSDFSADF", ForState:UIControlState.Normal)

Self.view.addSubview (Button3)

Common buttons from system-defined button types

Let Btn4:uibutton = UIButton (Type:UIButtonType.Custom)

Btn4.settitlecolor (Uicolor.blackcolor (), ForState:UIControlState.Normal)

Btn4.settitle ("12344444", ForState:UIControlState.Normal)

btn4.titlelabel!. Font = uifont.systemfontofsize (14)

Btn4.frame = CGRect (x:110, y:300, width:100, height:100)

Self.view.addSubview (BTN4)

Create a partial fillet button

Let Btn5:uibutton = UIButton (Type:UIButtonType.Custom)

Btn5.backgroundcolor = Uicolor.redcolor ()

Btn5.settitlecolor (Uicolor.blackcolor (), ForState:UIControlState.Normal)

Btn5.settitle ("345555", ForState:UIControlState.Normal)

btn5.titlelabel!. Font = uifont.systemfontofsize (14)

Btn5.frame = CGRect (x:110, y:400, width:100, height:100)

Self.view.addSubview (BTN5)

Uirectcornertopleft = 1 << 0,

Uirectcornertopright = 1 << 1,

Uirectcornerbottomleft = 1 << 2,

Uirectcornerbottomright = 1 << 3,

Uirectcornerallcorners = ~0ul

Let Beizer:uibezierpath = Uibezierpath (RoundedRect:btn5.bounds, byroundingcorners:uirectcorner.topleft| Uirectcorner.topright, Cornerradii:cgsize (width:15, height:15))

Let Beizer:uibezierpath = Uibezierpath (RoundedRect:btn5.bounds, byroundingcorners: [Uirectcorner.topleft, Uirectcorner.topright], cornerradii:cgsize (width:15, height:15))

Let Shape:cashapelayer = Cashapelayer ()

Shape.path = Beizer. Cgpath

Btn5.layer.mask = shape

Create Border button

Let Btn9:uibutton = UIButton (Frame:cgrect (x:50, y:500, width:100, height:35))

Btn9.backgroundcolor = Uicolor.whitecolor ()

Btn9.settitle ("Border button", ForState:UIControlState.Normal)

Btn9.settitlecolor (Uicolor.blackcolor (), ForState:UIControlState.Normal)

Btn9.layer.borderColor = Uicolor.blackcolor (). Cgcolor

Btn9.layer.borderWidth = 1

Btn9.layer.cornerRadius = 5

Self.view.addSubview (BTN9)

}

Func Press (Sender:uibutton) {

Print ("You pressed the button")

}

Override Func didreceivememorywarning () {

Super.didreceivememorywarning ()

Dispose of any resources the can be recreated.

}

}

The UIButton of Swift learning

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.