对于习惯了OC代码的程序员来说,swift的语法简直让人不能忍受,今天将一些常用的UI控件简单做了一下整理。
import UIKit class ViewController : UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.whiteColor() if self.title == "UILabel" { // Label let label = UILabel(frame: self.view.bounds) label.backgroundColor = UIColor.clearColor() label.textAlignment = NSTextAlignment.Center label.font = UIFont.systemFontOfSize(36) label.text = "Hello, Swift" self.view.addSubview(label) } else if self.title == "UIButton" { // Button let button = UIButton(type: UIButtonType.System) button.frame = CGRectMake(110, 120, 100, 50) button.backgroundColor = UIColor.grayColor() button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal) button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted) button.setTitle("普通", forState: UIControlState.Normal) button.setTitle("高亮", forState: UIControlState.Highlighted) button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside) button.tag = 100 self.view.addSubview(button) } else if self.title == "UIImageView" { let image = UIImage(named: "swift-hero.png") let imageView = UIImageView(frame: 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.title == "UISlider" { let slider = UISlider(frame:CGRectMake(60, 120, 200, 30)) self.view.addSubview(slider) } else if self.title == "UIWebView" { let webView = UIWebView(frame:self.view.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 == "UISegmentedControl" { let segmentControl = UISegmentedControl(items:["A", "B", "C", "D"]) segmentControl.frame = CGRectMake(110, 120, 100, 30) self.view.addSubview(segmentControl) } else if self.title == "UISwitch" { let switchControl = UISwitch(frame:CGRectMake(130, 120, 100, 30)) switchControl.on = true self.view.addSubview(switchControl) } else if self.title == "UITextField" { let textField = UITextField(frame:CGRectMake(60, 120, 200, 30)) textField.backgroundColor = UIColor.lightGrayColor() textField.placeholder = "请输入" self.view.addSubview(textField) } else if self.title == "UIScrollView" { let scrollView = UIScrollView(frame:CGRectMake(60, 120, 200, 200)) 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 += 200 view.backgroundColor = UIColor(red: CGFloat(100 * i) / 256, green: CGFloat(100 * i) / 256, blue: CGFloat(100 * i) / 256, alpha: 1) // 这个地方因为swift对于类型的控制十分严格,颜色值采用的是CGFloat类型,但是i是int类型,所以需要强转 scrollView.addSubview(view) } scrollView.contentSize = CGSizeMake(3 * 200, 0) self.view.addSubview(scrollView) } else if self.title == "UISearchBar" { let searchBar = UISearchBar(frame:CGRectMake(10.0, 120.0, 300.0, 30.0)) searchBar.showsCancelButton = true searchBar.searchBarStyle = UISearchBarStyle.Minimal // Default, Prominent, Minimal self.view.addSubview(searchBar) } else if self.title == "UIPageControl" { // PageControl let pageControl = UIPageControl(frame:CGRectMake(60.0, 120.0, 200.0, 200.0)) pageControl.numberOfPages = 5 pageControl.currentPageIndicatorTintColor = UIColor.blackColor() pageControl.pageIndicatorTintColor = UIColor.redColor() self.view.addSubview(pageControl) } else if self.title == "UIDatePicker" { let 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, 120.0, 300.0, 200.0)) pickerView.delegate = self pickerView.dataSource = self self.view.addSubview(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 == "UITextView" { let textView = UITextView(frame:CGRectMake(10.0, 120.0, 300.0, 200.0)) textView.backgroundColor = UIColor.lightGrayColor() textView.editable = false textView.font = UIFont.systemFontOfSize(20) textView.text = "Swift is an 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 into 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:CGRectMake(60.0, 120.0, 200.0, 30.0)) let flexibleSpace = UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.FlexibleSpace, target:nil, action:nil) let barBtnItemA = UIBarButtonItem(title: "A", style:UIBarButtonItemStyle.Plain, target:nil, action:nil) let barBtnItemB = UIBarButtonItem(title: "B", style:UIBarButtonItemStyle.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, flexibleSpace, barBtnItemC, flexibleSpace, barBtnItemD, flexibleSpace] self.view.addSubview(toolBar) } else if self.title == "UIActionSheet" { // Button let button = UIButton(type: UIButtonType.System) button.frame = CGRectMake(60.0, 120.0, 200.0, 50.0) button.backgroundColor = UIColor.grayColor() button.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: "showActionSheet", forControlEvents: UIControlEvents.TouchUpInside) button.tag = 101 self.view.addSubview(button) } else if self.title == "UIActivityIndicatorView" { let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle:UIActivityIndicatorViewStyle.Gray) activityIndicatorView.frame = CGRectMake(140.0, 120.0, 40.0, 40.0) activityIndicatorView.startAnimating() self.view.addSubview(activityIndicatorView) } } func buttonAction(sender: UIButton) { let alertController = UIAlertController(title: "通知", message: "确定还是取消", preferredStyle: UIAlertControllerStyle.Alert) let alertView1 = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in print("确定按钮点击事件") } let alertView2 = UIAlertAction(title: "取消", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in print("取消按钮点击事件") } let alertView3 = UIAlertAction(title: "下次吧", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in print("下次吧按钮点击事件") } alertController.addAction(alertView1) alertController.addAction(alertView2) alertController.addAction(alertView3) self.presentViewController(alertController, animated: true, completion: nil) } func showActionSheet() { let alertController = UIAlertController(title: "通知", message: "确定还是取消", preferredStyle: UIAlertControllerStyle.ActionSheet) let alertView1 = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in print("确定按钮点击事件") } let alertView2 = UIAlertAction(title: "取消", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in print("取消按钮点击事件") } let alertView3 = UIAlertAction(title: "下次吧", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in print("下次吧按钮点击事件") } 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.didReceiveMemoryWarning() } }
时间: 2024-10-18 17:13:36