Import UIKit
Class Viewcontroller:uiviewcontroller {
Declare a BTN
var examplebtn:uibutton!
Override Func Viewdidload () {
Super.viewdidload ()
Declaring a method
MAKEBTN ()
Additional setup after loading the view, typically from a nib.
}
Method
Private Func makebtn () {
Initialization
SELF.EXAMPLEBTN = Uibutton.init ()
Set frame
Self.exampleBtn.frame = CGRectMake (100, 100, 60, 60)
Set Background color
Self.exampleBtn.backgroundColor = Uicolor.lightgraycolor ()
Set BTN Font Size
Self.exampleBtn.titleLabel?. Font = uifont.systemfontofsize (12)
Set the text of a btn
Self.exampleBtn.setTitle ("Clickbnt", ForState:UIControlState.Normal)
Tagged tag
Self.exampleBtn.tag = 520
Add a Click event
Self.exampleBtn.addTarget (Self, Action: "Clickbtndown:", ForControlEvents:UIControlEvents.TouchUpInside)
Set up a picture of btn
Self.exampleBtn.setImage (UIImage (imageliteral: "103"), ForState:UIControlState.Normal)
Self.view. Addsubview (SELF.EXAMPLEBTN)
}
BTN Click events
Func Clickbtndown (Btn:uibutton) {
Print ("\ (Btn.tag)")
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
Dispose of any resources the can be recreated.
}
}
Swift_ui_btn of the iOS development series