How to use Swift common UI controls

Source: Internet
Author: User

For programmers accustomed to OC code, Swift's syntax is simply unbearable, and today some of the most common UI controls are simply collated.

Import Uikitclass Viewcontroller:uiviewcontroller, Uipickerviewdatasource, uipickerviewdelegate {override Func ViewD  Idload () {super.viewdidload () Self.view.backgroundColor = Uicolor.whitecolor () if Self.title = = "UILabel" {//Label let label = UILabel (frame:self.view.bounds) label.backgr Oundcolor = Uicolor.clearcolor () label.textalignment = Nstextalignment.center Label.font = UIFont.sy Stemfontofsize (Label.text) = "Hello, Swift" Self.view.addSubview (label)} else if s            Elf.title = = "UIButton" {//Button Let button = UIButton (Type:UIButtonType.System) Button.frame = CGRectMake ((+, +, +) Button.backgroundcolor = Uicolor.graycolor () button. Settitlecolor (Uicolor.redcolor (), ForState:UIControlState.Normal) Button.settitlecolor (Uicolor.whitecolor (), F Orstate:uicontrolstate.Highlighted) Button.settitle ("normal", ForState:UIControlState.Normal) Button.settitle ("Highlight", Forstate: uicontrolstate.highlighted) Button.addtarget (Self, Action: "Buttonaction:", FORCONTROLEVENTS:UICONTROLEVENTS.T Ouchupinside) Button.tag = Self.view.addSubview (button)} else if self.title = = "U Iimageview "{let-image = UIImage (named:" Swift-hero.png ") Let ImageView = Uiimageview (fr Ame:cgrectmake (Cgrectgetwidth (self.view.bounds)-image!. Size.width)/2.0, 120.0, image!. Size.width, image!. size.height)) Imageview.image = Image Self.view.addSubview (imageView)} else if self.t Itle = = "UISlider" {Let slider = UISlider (Frame:cgrectmake (+/-) Self.view.addS Ubview (Slider)} else if self.title = = "UIWebView" {Let WebView = UIWebView (frame:self.vi ew.bounds) Let URL = NSURL (string: "http://caipiao.m.taobao.com") Let request = Nsurlrequest (url:url!) Webview.loadrequest (Request) Self.view.addSubview (WebView)} else if self.title = = "Uisegmentedco Ntrol "{Let Segmentcontrol = Uisegmentedcontrol (items:[" A "," B "," C "," D "]) segmentcontrol.f  Rame = CGRectMake (+, +, +) Self.view.addSubview (Segmentcontrol)} else if self.title = = "Uiswitch" {Let Switchcontrol = Uiswitch (Frame:cgrectmake (*, +, +)) Switchcontrol            . On = True Self.view.addSubview (Switchcontrol)} else if self.title = = "Uitextfield" { Let TextField = Uitextfield (Frame:cgrectmake (max., Max.)) Textfield.backgroundcolor = Uicolor.lig Htgraycolor () Textfield.placeholder = "Please enter" Self.view.addSubview (TextField)} else if       Self.title = = "Uiscrollview" {     Let ScrollView = Uiscrollview (Frame:cgrectmake (max, Max, max)) scrollview.pagingenabled = True  Scrollview.showsverticalscrollindicator = False Self.view.addSubview (ScrollView) var Fx:cgfloat = 0.0 for (var i = 0; i < 3; ++i) {Let view = UIView (frame:cgrectmake                (FX, 0, 200, 200)) FX + = $ view.backgroundcolor = Uicolor (red:cgfloat (+ * i)/green:cgfloat (+/-)/+/-Blue:                CGFloat (+ * i)/alpha:1)//This place because Swift controls the type very strictly, the color value takes the CGFloat type, but I is the int type, therefore needs the strong turn Scrollview.addsubview (view)} scrollview.contentsize = Cgsizemake (3 * 0) Self.view . Addsubview (ScrollView)} else if self.title = = "Uisearchbar" {Let Searchbar = Uisearchba R (Frame:cgrectmake (10.0, 120.0, 300.0, 30.0)) Searchbar.showscancelbutton = True Searchbar. Searchbarstyle = uisearchbarstyle.minimal//Default, prominent, Minimal Self.view.addSubview (Sear  Chbar)} else if self.title = = "Uipagecontrol" {//Pagecontrol let Pagecontrol = Uipagecontrol (Frame:cgrectmake (60.0, 120.0, 200.0, 200.0)) Pagecontrol.numberofpages = 5 Pagecont Rol.currentpageindicatortintcolor = Uicolor.blackcolor () Pagecontrol.pageindicatortintcolor = UIColor.redColor ( ) Self.view.addSubview (Pagecontrol)} else if self.title = = "Uidatepicker" {le        T datePicker = Uidatepicker (Frame:cgrectmake (0.0, 120.0, 200.0, 200.0)) Self.view.addSubview (DatePicker) } else if self.title = = "Uipickerview" {Let Pickerview = Uipickerview (Frame:cgrectmake (10.0, 12 0.0, 300.0, 200.0)) pickerview.delegate = self Pickerview.datasource = self Self.view.add     Subview (Pickerview)   } else if self.title = = "Uiprogressview" {Let Progressview = Uiprogressview (progressviewstyle : uiprogressviewstyle.default) Progressview.frame = CGRectMake (10.0, 120.0, 300.0, 30.0) Progressview . setprogress (0.8, Animated:true) Self.view.addSubview (Progressview)} else if self.title = = "UIT Extview "{Let TextView = Uitextview (Frame:cgrectmake (10.0, 120.0, 300.0, 200.0)) textview.ba Ckgroundcolor = Uicolor.lightgraycolor () Textview.editable = False Textview.font = Uifont.systemfont Ofsize Textview.text = "Swift is a innovative new programming language for Cocoa and Cocoa Touch. Writing code is interactive and fun, the syntax is concise yet expressive, and apps run Lightning-fast. Swift is ready for your next IOS and OS X project-or for addition to your current App-because Swift code works side-            By-side with Objective-c. " Self.view.adDsubview (TextView)} else if self.title = = "Uitoolbar" {Let ToolBar = Uitoolbar (frame:cgr Ectmake (60.0, 120.0, 200.0, 30.0)) Let Flexiblespace = Uibarbuttonitem (barbuttonsystemitem:uibarbut Tonsystemitem.flexiblespace, Target:nil, action:nil) Let Barbtnitema = Uibarbuttonitem (title: "A", Style:uibarb Uttonitemstyle.plain, Target:nil, action:nil) Let Barbtnitemb = Uibarbuttonitem (title: "B", Style:uibarbuttonit Emstyle.plain, Target:nil, action:nil) Let Barbtnitemc = Uibarbuttonitem (title: "C", Style:uibarbuttonitemstyle . Plain, Target:nil, action:nil) Let barbtnitemd = Uibarbuttonitem (title: "D", Style:UIBarButtonItemStyle.Plain, Target:nil, action:nil) Toolbar.items = [Flexiblespace, Barbtnitema, Flexiblespace, Barbtnitemb, FL        Exiblespace, BARBTNITEMC, Flexiblespace, Barbtnitemd, Flexiblespace] Self.view.addSubview (ToolBar)} else if Self.title= = "Uiactionsheet" {//Button Let button = UIButton (Type:UIButtonType.System) BU Tton.frame = CGRectMake (60.0, 120.0, 200.0, 50.0) Button.backgroundcolor = Uicolor.graycolor () butto  N.settitlecolor (Uicolor.redcolor (), ForState:UIControlState.Normal) Button.settitlecolor (Uicolor.whitecolor (),            forState:UIControlState.Highlighted) button.settitle ("Actionsheet", ForState:UIControlState.Normal) Button.settitle ("Actionsheet", forState:UIControlState.Highlighted) Button.addtarget (Self, Action: "Showact Ionsheet ", forControlEvents:UIControlEvents.TouchUpInside) Button.tag = 101 Self.view.addSubview (bu Tton)} else if self.title = = "Uiactivityindicatorview" {Let Activityindicatorview = Uiac Tivityindicatorview (activityIndicatorStyle:UIActivityIndicatorViewStyle.Gray) activityindicatorview.frame = CGR Ectmake (140.0, 120.0, 40.0, 40.0) activityindicatorview.startanimating () Self.view.addSubview (Activityindicatorview) }} func Buttonaction (Sender:uibutton) {Let Alertcontroller = Uialertcontroller (title: "Notification", MESSAG E: "OK or Cancel", preferredStyle:UIAlertControllerStyle.Alert) Let AlertView1 = uialertaction (title: "OK", Style:uiale Rtactionstyle.default) {(uialertaction)-Void in print ("OK button click event")} Let AlertView2 = Uiale        Rtaction (title: "Cancel", Style:UIAlertActionStyle.Default) {(uialertaction), Void in print ("Cancel button click event") } Let ALERTVIEW3 = Uialertaction (title: "Next Time", Style:UIAlertActionStyle.Default) {(uialertaction), Void I N Print ("Next button click event")} alertcontroller.addaction (AlertView1) alertcontroller.addaction (ale RTVIEW2) alertcontroller.addaction (ALERTVIEW3) Self.presentviewcontroller (Alertcontroller, Animated:true, C    Ompletion:nil)}    Func Showactionsheet () {Let Alertcontroller = Uialertcontroller (title: "Notification", message: "OK or Cancel", PREFERREDST Yle:UIAlertControllerStyle.ActionSheet) Let AlertView1 = uialertaction (title: "OK", style:uialertactionst Yle. Default) {(uialertaction)-Void in print ("OK button click event")} Let AlertView2 = Uialertaction (title : "Cancel", Style:UIAlertActionStyle.Default) {(uialertaction), Void in print ("Cancel button click event")} le T alertView3 = uialertaction (title: "Next Time", Style:UIAlertActionStyle.Default) {(uialertaction), Void in P        Rint ("Next button click event")} alertcontroller.addaction (AlertView1) alertcontroller.addaction (ALERTVIEW2) Alertcontroller.addaction (ALERTVIEW3) Self.presentviewcontroller (Alertcontroller, Animated:true, Completion:nil    )}//Uipickerviewdatasource Methods//Returns the number of ' columns ' to display. Func Numberofcomponentsinpickerview (PickeRview:uipickerview)-Int {return 3}//Returns the # of rows in each component.        Func Pickerview (Pickerview:uipickerview, Numberofrowsincomponent component:int), Int {return 10}    Func Pickerview (Pickerview:uipickerview, Titleforrow row:int, Forcomponent component:int), String? {return String (format: "%i", Row)} override func didreceivememorywarning () {Super.didreceivememorywa Rning ()}}

How to use Swift common UI controls

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.