Swift 简单控件示例:搜索框(UISearchBar)

前端之家收集整理的这篇文章主要介绍了Swift 简单控件示例:搜索框(UISearchBar)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
转载请声明出处:http://www.jb51.cc/article/p-savwrnzi-ga.html
------------------------------------------------------------------------------------------

//
//  ViewController.swift
//  UISearchBarSample
//
//  Created by jinnchang on 15/4/2.
//  Copyright (c) 2015年 Jinn Chang. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UISearchBarDelegate {
    
    var searchBar: UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.
        
        searchBar = UISearchBar(frame: CGRectMake(0,20,self.view.frame.size.width,80))
        searchBar.placeholder = "search"
        searchBar.barStyle = .Default
        searchBar.showsBookmarkButton = true
        searchBar.showsCancelButton = true
        searchBar.delegate = self
        
        self.view.addSubview(searchBar)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // 每次输入内容改变触发事件
    func searchBar(searchBar: UISearchBar,textDidChange searchText: String) {
        println("筛选内容:\(searchText)")
    }
    
    // 书签按钮触发事件
    func searchBarBookmarkButtonClicked(searchBar: UISearchBar) {
        println("搜索历史记录")
    }
    
    // 取消按钮触发事件
    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        println("取消搜索")
    }
    
    // 搜索触发事件
    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        println("开始搜索")
    }

}
------------------------------------------------------------------------------------------
GitHub 上项目地址:UISearchBarSample
控件更多相关属性方法参考:UISearchBar Class Reference 文章最后更新时间:2015年4月3日11:42:05

猜你在找的Swift相关文章