一个、总结

使用Swift去完成iOS的UI接口,事实上,目前的想法和OC实现几乎一致,只是在措辞非常大的差异,修改前更更大的个人控制。为有纯代码强迫症,所以接下来创建一个纯代码动项目,然后在此基础上具体描写叙述经常使用UI控件的Swift代码实现。

二、创建项目

首先创建一个新项目SwiftDemo,语言选择Swift,详情參考《 iOS8开发~Swift(一)入门》,然后打开项目plist文件。删除当中的Main
storyboard file base name一行。其它内容不须要修改。程序会默认从AppDelegate.swift開始运行,但此时主window须要代码初始化。由于我们已经删除了storyboard。所以须要加入例如以下代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible() return true
}

如今程序应该能够执行了,只是什么内容都没有。

以下打算搭建一个非经常见都项目框架。主window的rootviewController为一个

UITabBarController对象,UITabBarController对象作为容器,其有两个标签,每一个标签的内容展示为一个

UINavigationController对象。UINavigationController对象的rootViewController为实际的用户界面。相信这个结构使用OC实现大家都非常熟悉了,但使用Swift该怎么样实现呢,事实上思想都是一样都,仅仅只是写法有差别而已,以下具体讨论代码实现了。

三、UI实现具体解释

1、在应用程序启动函数中加入例如以下代码:当中 viewControllerA 和 viewControllerB是需新创建都两个视图控制器,详细实现參考《 iOS8开发~Swift(一)入门》

var window: UIWindow?
var tabBarController: UITabBarController? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary? ) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.backgroundColor = UIColor.whiteColor() var viewControllerA = ViewControllerA();
viewControllerA.title = "基本UI" var viewControllerB = ViewControllerB();
viewControllerB.title = "其它" self.tabBarController = UITabBarController()
self.tabBarController!.delegate = self;
self.tabBarController!.viewControllers = [
UINavigationController(rootViewController: viewControllerA),
UINavigationController(rootViewController: viewControllerB)
]
self.tabBarController!.selectedIndex = 0; self.window!.rootViewController = self.tabBarController
self.window!.makeKeyAndVisible() return true
}

这样。之前描写叙述的项目框架就实现了。

假设须要在UITabBarController的代理方法中做一些事情,能够实现其代理:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate
//UITabBarControllerDelegate
func tabBarController(tabBarController: UITabBarController!, didSelectViewController viewController: UIViewController!) { }

执行效果:

2、以ViewControllerA作为UI介绍导航页面,在当中实现一个表视图TableView,其数据源从读取一个plist文件获取:

新建立一个ViewControllerA,取消勾选Xib选项。创建后重写loadView方法,然后初始化一个表视图。主要代码例如以下:

import UIKit

class ViewControllerA: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var list: NSArray?
var tableView: UITableView? override func loadView() {
super.loadView()
} override func viewDidLoad() {
super.viewDidLoad() self.list = NSArray(contentsOfFile: NSBundle.mainBundle().pathForResource("BasicUI", ofType:"plist"))
println(self.list) self.tableView = UITableView(frame: self.view.frame, style:UITableViewStyle.Plain)
self.tableView!.delegate = self
self.tableView!.dataSource = self
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
self.view?.addSubview(self.tableView) } func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return 1;
} func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return self.list!.count
} func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell!
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
cell.textLabel.text = self.list? .objectAtIndex(indexPath.row) as String return cell
} // UITableViewDelegate Methods
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
{
self.tableView!.deselectRowAtIndexPath(indexPath, animated: true) var itemString = self.list?.objectAtIndex(indexPath.row) as String var viewController : UIViewController?
if itemString == "UIView" {
viewController = TheViewController()
} //省略部分代码
else {
println("viewController is nil")
} viewController!.hidesBottomBarWhenPushed = true
self.navigationController.pushViewController(viewController, animated:true)
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated.
}
}

实际执行效果

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl6aG9uZ2Z1MjAxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

3、以下逐个介绍各个控件的实现

(1)UIView

import UIKit

class TheView: UIView {
override func drawRect(rect: CGRect) {
UIColor.redColor().setFill();
UIRectFill(CGRectMake(0, 100, 100, 100));
}
} class TheViewController: UIViewController { override func loadView() {
let theView = TheView()
theView.backgroundColor = UIColor.brownColor()
theView.layer.cornerRadius = 3
theView.layer.borderWidth = 5
theView.layer.borderColor = UIColor.darkGrayColor().CGColor self.view = theView;
} override func viewDidLoad() {
super.viewDidLoad() // 加入手势
let recognizer = UITapGestureRecognizer(target: self, action: "tapAction:")
recognizer.numberOfTapsRequired = 2;
recognizer.numberOfTouchesRequired = 1;
self.view.addGestureRecognizer(recognizer)
} func tapAction(sender: UIGestureRecognizer)
{
println("tapAction")
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

当中涉及到视图手势加入,视图重会,改动Layer等实现,效果如图:

(2)UILabel

import UIKit

class LabelViewController: UIViewController {

    override func loadView() {
super.loadView();
} override func viewDidLoad() {
super.viewDidLoad() var label = UILabel(frame: CGRectMake(0, 100, self.view.bounds.size.width, 200))
label.backgroundColor = UIColor.blackColor()
label.textColor = UIColor.whiteColor()
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.text = "Swift is designed to provide seamless compatibility with Cocoa and Objective-C. You can use Objective-C APIs (ranging from system frameworks to your own custom code) in Swift, and you can use Swift APIs in Objective-C. "
label.font = UIFont.italicSystemFontOfSize(20) self.view.addSubview(label)
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

效果如图:

(3)UIButton

import UIKit

class ButtonViewController: UIViewController {

    override func loadView() {
super.loadView();
} override func viewDidLoad() {
super.viewDidLoad() // Do any additional setup after loading the view. var button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRect(x:100, y:200, width:100, height:100);
button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
button.setTitleColor(UIColor.greenColor(), forState:UIControlState.Selected)
button.setTitle("Normal", forState:UIControlState.Normal)
button.setTitle("Selected", forState:UIControlState.Selected)
button.setBackgroundImage(UIImage(named: "image.png"), forState:UIControlState.Normal)
button.tag = 1000;
button.clipsToBounds = true;
button.layer.cornerRadius = 5;
button.addTarget(self, action:"buttonAction:", forControlEvents:UIControlEvents.TouchUpInside) self.view.addSubview(button)
} func buttonAction(sender: UIButton) {
println("buttonAction")
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

执行效果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl6aG9uZ2Z1MjAxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

(4)UIImageView 与 UIImage

import UIKit

class ImageViewController: UIViewController {

    override func loadView() {
super.loadView();
} override func viewDidLoad() {
super.viewDidLoad() // Do any additional setup after loading the view. var imageView = UIImageView(frame: CGRectMake(100, 100, 50, 50))
imageView.backgroundColor = UIColor.whiteColor();
imageView.animationImages = [
UIImage(contentsOfFile: NSBundle.mainBundle().pathForResource("bird-01", ofType:"png")),
UIImage(contentsOfFile: NSBundle.mainBundle().pathForResource("bird-02", ofType:"png")),
UIImage(contentsOfFile: NSBundle.mainBundle().pathForResource("bird-03", ofType:"png")),
UIImage(named: "bird-04.png")
]
imageView.contentMode = UIViewContentMode.ScaleAspectFit
imageView.animationDuration = 1;
imageView.startAnimating() self.view.addSubview(imageView)
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

执行效果,这是一个图片动画:

(5)UITextField

import UIKit

class TextFieldViewController: UIViewController, UITextFieldDelegate {

    override func loadView() {
super.loadView();
} override func viewDidLoad() {
super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: "textFieldTextDidBeginEditingNotification:", name:UITextFieldTextDidBeginEditingNotification, object:nil) var textField = UITextField(frame: CGRectMake(10, 100, 300, 40))
textField.backgroundColor = UIColor.clearColor()
textField.textColor = UIColor.blackColor()
textField.placeholder = "请输入..."
textField.borderStyle = UITextBorderStyle.RoundedRect
textField.adjustsFontSizeToFitWidth = true
textField.delegate = self self.view.addSubview(textField)
} func textFieldTextDidBeginEditingNotification(sender: NSNotification!) { } // UITextFieldDelegate
func textFieldShouldBeginEditing(textField: UITextField!) -> Bool {
return true;
} func textFieldDidBeginEditing(textField: UITextField!) {
} func textFieldShouldEndEditing(textField: UITextField!) -> Bool {
return true;
} func textFieldDidEndEditing(textField: UITextField!) {
} func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
return true;
} func textFieldShouldReturn(textField: UITextField!) -> Bool {
return true;
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

执行效果:

(6)UITextView

import UIKit

class TextViewController: UIViewController, UITextViewDelegate {

    override func loadView() {
super.loadView();
} override func viewDidLoad() {
super.viewDidLoad() var textView = UITextView(frame:CGRectMake(10.0, 120.0, 300.0, 400.0))
textView.backgroundColor = UIColor.blackColor()
textView.textColor = UIColor.whiteColor()
textView.editable = false
textView.font = UIFont.boldSystemFontOfSize(30)
textView.delegate = self;
textView.text = "Swift is designed to provide seamless compatibility with Cocoa and Objective-C. You can use Objective-C APIs (ranging from system frameworks to your own custom code) in Swift, and you can use Swift APIs in Objective-C. "
self.view.addSubview(textView)
} func textViewShouldBeginEditing(textView: UITextView!) -> Bool {
return true;
} func textViewShouldEndEditing(textView: UITextView!) -> Bool {
return true;
} func textViewDidBeginEditing(textView: UITextView!) {
} func textViewDidEndEditing(textView: UITextView!) {
} func textView(textView: UITextView!, shouldChangeTextInRange range: NSRange, replacementText text: String!) -> Bool {
return true;
} func textViewDidChange(textView: UITextView!) {
} func textViewDidChangeSelection(textView: UITextView!) {
} func textView(textView: UITextView!, shouldInteractWithURL URL: NSURL!, inRange characterRange: NSRange) -> Bool {
return true;
} func textView(textView: UITextView!, shouldInteractWithTextAttachment textAttachment: NSTextAttachment!, inRange characterRange: NSRange) -> Bool {
return true;
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }

执行效果:

(7)UISwitch

import UIKit

class SwitchViewController: UIViewController {

    override func loadView() {
super.loadView();
} override func viewDidLoad() {
super.viewDidLoad() var switchCtr = UISwitch(frame: CGRectMake(100, 100, 80, 30))
switchCtr.on = true
switchCtr.addTarget(self, action: "switchAction:", forControlEvents:UIControlEvents.ValueChanged)
self.view.addSubview(switchCtr)
} func switchAction(sender: UISwitch) {
println("switchAction")
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }

执行效果:

(8)UIScrollView

import UIKit

class ScrollViewController: UIViewController, UIScrollViewDelegate {

    override func loadView() {
super.loadView();
} override func viewDidLoad() {
super.viewDidLoad() var scroll = UIScrollView(frame: self.view.bounds)
scroll.pagingEnabled = true
scroll.scrollEnabled = true
scroll.showsVerticalScrollIndicator = true
scroll.showsHorizontalScrollIndicator = true
scroll.delegate = self var X : CGFloat = 0
for (var i = 0; i < 2; i++) {
var label = UILabel()
label.text = "\(i)"
label.textAlignment = NSTextAlignment.Center
label.backgroundColor = UIColor.lightGrayColor()
label.frame = CGRectMake(X, 0, self.view.bounds.size.width, self.view.bounds.size.height)
X += 320
scroll.addSubview(label)
} scroll.contentSize = CGSizeMake(2 * self.view.bounds.size.width, self.view.bounds.size.height) self.view.addSubview(scroll)
} // UIScrollViewDelegate
func scrollViewDidScroll(scrollView: UIScrollView!) {
println("scrollViewDidScroll")
} func scrollViewWillBeginDragging(scrollView: UIScrollView!) {
} func scrollViewWillEndDragging(scrollView: UIScrollView!, withVelocity velocity: CGPoint, targetContentOffset: CMutablePointer<CGPoint>) {
} func scrollViewDidEndDragging(scrollView: UIScrollView!, willDecelerate decelerate: Bool) { } func scrollViewWillBeginDecelerating(scrollView: UIScrollView!) {
} func scrollViewDidEndDecelerating(scrollView: UIScrollView!) {
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }

执行效果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl6aG9uZ2Z1MjAxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

(9)UIPageView

import UIKit

class PageControlViewController: UIViewController {

    override func loadView() {
super.loadView()
} override func viewDidLoad() {
super.viewDidLoad() var pageControl = UIPageControl(frame: CGRectMake(100, 100, 100, 100));
pageControl.numberOfPages = 10
pageControl.currentPage = 5
pageControl.currentPageIndicatorTintColor = UIColor.grayColor()
pageControl.pageIndicatorTintColor = UIColor.redColor()
pageControl.addTarget(self, action: "pageControlAction:", forControlEvents:UIControlEvents.ValueChanged) self.view.addSubview(pageControl)
} func pageControlAction(sender: UIPageControl) {
println("pageControlAction:\(sender.currentPage)")
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

效果如图:

(10)UIAlertView 与 UIActionSheet

import UIKit

class AlertViewController: UIViewController {

    override func loadView() {
super.loadView()
} override func viewDidAppear(animated: Bool) { let alertController = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.Alert) // let alertController = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.ActionSheet) let cancelAction = UIAlertAction(title: "cancel", style: UIAlertActionStyle.Cancel) { action in
println("cancel")
} let otherAction = UIAlertAction(title: "other", style: UIAlertActionStyle.Default) { action in
NSLog("other")
} let secondAction = UIAlertAction(title: "secondAction", style: UIAlertActionStyle.Default) { action in
NSLog("secondAction")
} alertController.addAction(cancelAction)
alertController.addAction(otherAction)
alertController.addAction(secondAction) presentViewController(alertController, animated: true, completion: nil)
} override func viewDidLoad() {
super.viewDidLoad() } override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

执行效果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl6aG9uZ2Z1MjAxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">     

(11)UIActivityIndicatorView

import UIKit

class ActivityIndicatorViewController: UIViewController {

    override func loadView() {
super.loadView()
} override func viewDidAppear(animated: Bool) { var activity = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
activity.frame = CGRectMake(100, 100, 40, 40)
activity.startAnimating()
activity.hidesWhenStopped = true self.view.addSubview(activity)
} override func viewDidLoad() {
super.viewDidLoad()
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }

执行效果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl6aG9uZ2Z1MjAxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

(12)UISlider

import UIKit

class SliderViewController: UIViewController {

    override func loadView() {
super.loadView()
} override func viewDidLoad() {
super.viewDidLoad() var slider = UISlider(frame:CGRectMake(10.0, 150.0, 300.0, 30.0))
slider.addTarget(self, action:"sliderAction:", forControlEvents:UIControlEvents.ValueChanged)
self.view.addSubview(slider)
} func sliderAction(sender: UISlider) {
println("sliderAction:\(sender.value)")
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

执行效果:

(13)UIProgressView

import UIKit

class ProgressViewController: UIViewController {

    override func loadView() {
super.loadView()
} override func viewDidAppear(animated: Bool) {
var progress = UIProgressView(progressViewStyle:UIProgressViewStyle.Default)
progress.progressTintColor = UIColor.blackColor()
progress.trackTintColor = UIColor.redColor()
progress.frame = CGRectMake(10.0, 150.0, 300.0, 40.0)
progress.setProgress(0.9, animated: true)
self.view.addSubview(progress)
} override func viewDidLoad() {
super.viewDidLoad()
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

效果如图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl6aG9uZ2Z1MjAxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

(14)UISegmentedControl

import UIKit

class SegmentedViewController: UIViewController {

    override func loadView() {
super.loadView()
} override func viewDidLoad() {
super.viewDidLoad() var segment = UISegmentedControl(items:["one", "two", "three", "four"])
segment.frame = CGRectMake(10.0, 110.0, 300.0, 30.0)
segment.segmentedControlStyle = UISegmentedControlStyle.Bordered
segment.momentary = true
segment.addTarget(self, action:"segmentAction:", forControlEvents:UIControlEvents.TouchUpInside) self.view.addSubview(segment)
} func segmentAction(sender: UISegmentedControl) {
println("segmentAction")
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }

实现效果:

(15)UIDatePicker

import UIKit

class DatePickerViewController: UIViewController {

    override func loadView() {
super.loadView()
} override func viewDidLoad() {
super.viewDidLoad() var datePicker = UIDatePicker(frame:CGRectMake(0.0, 120.0, 200.0, 200.0))
datePicker.datePickerMode = UIDatePickerMode.DateAndTime
datePicker.minimumDate = NSDate.date()
datePicker.minuteInterval = 1
datePicker.addTarget(self, action: "action", forControlEvents: UIControlEvents.ValueChanged) self.view.addSubview(datePicker)
} func action() {
println("action")
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }

实现效果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl6aG9uZ2Z1MjAxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

(16)UIWebView

import UIKit

class WebViewController: UIViewController, UIWebViewDelegate {

    override func loadView() {
super.loadView()
} override func viewDidLoad() {
super.viewDidLoad() var webView = UIWebView(frame: self.view.bounds)
webView.backgroundColor = UIColor.whiteColor()
webView.scalesPageToFit = true
webView.delegate = self; var url = NSURL(string: "http://www.baidu.com")
var request = NSURLRequest(URL: url) webView.loadRequest(request) self.view.addSubview(webView)
} // UIWebViewDelegate
func webViewDidStartLoad(webView: UIWebView) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
} func webViewDidFinishLoad(webView: UIWebView) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
} func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
println("didFailLoadWithError")
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
} func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

实现效果:

(17)UIToolbar

import UIKit

class ToolbarViewController: UIViewController {

    override func loadView() {
super.loadView()
} override func viewDidLoad() {
super.viewDidLoad() var toolBar = UIToolbar(frame:CGRectMake(10.0, 120.0, 300.0, 30.0))
toolBar.barStyle = .BlackTranslucent
toolBar.tintColor = UIColor.greenColor()
toolBar.backgroundColor = UIColor.blueColor() var flexibleSpace = UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.FlexibleSpace, target:"barButtonItemClicked:", action:nil)
var barBtnItemA = UIBarButtonItem(title: "one", style:UIBarButtonItemStyle.Plain, target:self, action:"barButtonItemClicked:")
var barBtnItemB = UIBarButtonItem(title: "two", style:UIBarButtonItemStyle.Plain, target:self, action:"barButtonItemClicked:")
var barBtnItemC = UIBarButtonItem(title: "three", style:UIBarButtonItemStyle.Plain, target:self, action:"barButtonItemClicked:")
var barBtnItemD = UIBarButtonItem(title: "four", style:UIBarButtonItemStyle.Plain, target:self, action:"barButtonItemClicked:") toolBar.items = [flexibleSpace, barBtnItemA, flexibleSpace, barBtnItemB, flexibleSpace, barBtnItemC, flexibleSpace, barBtnItemD, flexibleSpace] self.view.addSubview(toolBar)
} func barButtonItemClicked(sender: UIBarButtonItem) {
NSLog("barButtonItemClicked: \(sender)")
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

实现效果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl6aG9uZ2Z1MjAxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

(18)UISearchBar

import UIKit

class SearchBarViewController: UIViewController, UISearchBarDelegate {

    override func loadView() {
super.loadView()
} override func viewDidLoad() {
super.viewDidLoad() var searchBar = UISearchBar(frame:CGRectMake(0, 60.0, 320.0, 100.0))
searchBar.showsCancelButton = true
searchBar.searchBarStyle = UISearchBarStyle.Default
searchBar.showsScopeBar = true
searchBar.scopeButtonTitles = [
"scope A",
"scope B"
] self.view.addSubview(searchBar)
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} // UISearchBarDelegate
func searchBar(UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
println("selectedScopeButtonIndexDidChange \(selectedScope)")
} func searchBarSearchButtonClicked(searchBar: UISearchBar) {
println("searchBarSearchButtonClicked: \(searchBar.text)") searchBar.resignFirstResponder()
} func searchBarCancelButtonClicked(searchBar: UISearchBar) {
println("searchBarCancelButtonClicked")
searchBar.resignFirstResponder()
} }

实现效果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl6aG9uZ2Z1MjAxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

到这里基本经常使用到UI控件实现的Swift版本号就搞定啦,当然实际项目中须要更灵活、更复杂的实现。这些代码仅供參考,希望有抛砖引玉的效果!很多其它内容请点击这里

Demo下载:http://download.csdn.net/detail/zfpp25_/7463851

欢迎增加群共同学习和进步:QQ群:170549973

版权声明:本文博客原创文章,博客,未经同意,不得转载。

iOS8发展~Swift(三)UI详细解释的更多相关文章

  1. Action的三种实现方式,struts.xml配置的详细解释及其简单执行过程(二)

    勿以恶小而为之,勿以善小而不为--------------------------刘备 劝诸君,多行善事积福报,莫作恶 上一章简单介绍了Struts2的'两个蝴蝶飞,你好' (一),如果没有看过,请观 ...

  2. .htaccess语法之RewriteCond与RewriteRule指令格式详细解释

    htaccess语法之RewriteCond与RewriteRule指令格式详细解释 (2012-11-09 18:09:08) 转载▼ 标签:  htaccess it 分类: 网络 上文htacc ...

  3. tar命令的详细解释

    tar命令的详细解释 标签: linuxfileoutputbashinputshell 2010-05-04 12:11 235881人阅读 评论(12) 收藏 举报  分类: linux/unix ...

  4. 设计模式 - 迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释

    迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考迭代器模式(ite ...

  5. Android slidingmenu详细解释 滑动的优化

    Android slidingmenu 详细解释 性能优化 转载请注明:   http://blog.csdn.net/aaawqqq 简单介绍 SlidingMenu 是github 上Androi ...

  6. Nginx location配置详细解释

    nginx location配置详细解释 语法规则: location [=|~|~*|^~] /uri/ { - } = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 ur ...

  7. Java进阶(十五)Java中设置session的详细解释

    Java中设置session的详细解释 简单通俗的讲session就是象一个临时的容器,用来存放临时的东西.从你登陆开始就保存在session里,当然你可以自己设置它的有效时间和页面,举个简单的例子: ...

  8. 用python进行OpenCV实战之用OpenCV3实现图片载入、显示和储存(argparse详细解释)

    将下面文档存为load_display_save.py #-*- coding:utf-8 -*- ap = argparse.ArgumentParser() ap.add_argument(&qu ...

  9. Linux下函数调用堆栈帧的详细解释【转】

    转自:http://blog.chinaunix.net/uid-30339363-id-5116170.html 原文地址:Linux下函数调用堆栈帧的详细解释 作者:cssjtuer http:/ ...

随机推荐

  1. 依据二度人脉推荐好友sql

    friend表结构 DROP TABLE IF EXISTS FRIEND; create table friend(     uid        bigint not null comment ' ...

  2. GDAL切割重采样遥感图像

    一个小测试程序开发全过程实录,完全新手入门级的实例,如果你还在为处理大影像而发愁,来试试这个称手的工具吧. Imagec 开发日记 2013-6-25 需求: 影像数据切割,重采样 数据切割的要求是简 ...

  3. Android在如何建立一个WebServer

    今天老板交待任务最终完成了,感觉收获颇多,所以写一个关于它的记录,首先,看一下.老板的需求 需求: 希望移动端的用户标识(IMEI)和HTML页面的用户标识(Cookie)连接起来,当中HTML页面可 ...

  4. Web测试基于实际测试的功能测试点总结--转载

    文章来源:http://www.51testing.com/html/99/n-854599.html 好文章就该记录一下\(^o^)/~ 一.页面链接检查:测试每一个链接是否都有对应的页面,并且页面 ...

  5. HDU1176_免费馅饼【号码塔】

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  6. Cocos2d-x 3.0 红孩儿私人义务教育 - 第一章 熟人Cocos2d-x 3.0project

     Cocos2d-x 3.0 红孩儿私家必修 前言: 时光飞逝,每每看到博客上的回复和微博上的鼓舞,总会认为亏欠大家点什么.停下来太久.总是认为不太对劲,哈哈,时习之吧,望以此勉励大家. 红孩儿C ...

  7. postgresql数据库配置csv格式的日志输出

    postgresql数据库配置csv格风格日志输出 以下介绍postgresql数据库中关于csv格式日志(pg中一种比較具体的日志输出方式)的设置方法. 1.进入$PGDATA文件夹(pg的安装文件 ...

  8. 雷人的一幕:国外的codeproject论坛竟有人发“中文贴”.....

    潜水近一年,头一次见国人在此发“中文贴”,截图留个“纪念”....

  9. Android应用开发-小巫CSDN博客client之显示博文具体内容

    Android应用开发-小巫CSDN博客客户端之显示博文具体内容 上篇博文给大家介绍的是怎样嵌入有米广告而且获取收益,本篇博客打算讲讲关于怎样在一个ListView里显示博文的具体信息.这个可能是童鞋 ...

  10. PHP 类属性 类静态变量的访问

    php的类属性其实有两种,一种是类常量,一种是类静态变量.两种容易引起混淆. 如同静态类方法和类实例方法一样,静态类属性和实例属性不能重定义(同名),但静态属性可以和类常量同名. <?php c ...