码迷,mamicode.com
首页 > 编程语言 > 详细

论 Swift 开发入门:搜索框(UISearchBar)

时间:2015-04-02 10:27:16      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:ios   swift   搜索框   uisearchbar   

转载请声明出处:http://blog.csdn.net/jinnchang/article/details/44827923
------------------------------------------------------------------------------------------

代码示例

//
//  ViewController.swift
//  UISearchBarSample
//
//  Created by jinnchang on 15/4/1.
//  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.prompt = "prompt"
        searchBar.text = "text"
        searchBar.barStyle = UIBarStyle.Default
        searchBar.searchBarStyle = UISearchBarStyle.Default
        searchBar.barTintColor = UIColor.orangeColor()
        searchBar.tintColor = UIColor.redColor()
        searchBar.translucent = true
        searchBar.showsBookmarkButton = true
        searchBar.showsCancelButton = true
        searchBar.showsSearchResultsButton = false
        searchBar.showsScopeBar = false
        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

文章最后更新时间:2015年4月2日09:35:35。更多资料参考:

UISearchBar Class Reference

UIKit User Interface Catalog: Search Bars

论 Swift 开发入门:搜索框(UISearchBar)

标签:ios   swift   搜索框   uisearchbar   

原文地址:http://blog.csdn.net/jinnchang/article/details/44827923

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!