貓貓學iOS 之微博項目實戰(5)微博自己定義搜尋方塊searchBar

來源:互聯網
上載者:User

標籤:推薦   pre   com   ima   原始碼   rect   sea   self   views   

貓貓分享。必須精品

原創文章。歡迎轉載。

轉載請註明:翟乃玉的部落格
地址:http://blog.csdn.net/u013357243

一:效果

用UITextField簡單定義一個搜尋方塊

二:調用:

調用的代碼。非常easy,直接init就能夠,以後加功能自己加入即可了。

- (void)viewDidLoad {    [super viewDidLoad];    // 建立搜尋方塊    NYSearchBar *searchBar = [[NYSearchBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 35)];    searchBar.placeholder = @"貓貓搜尋";    // 設定titleView為搜尋方塊    self.navigationItem.titleView = searchBar;}
三:代碼:

NYSearchBar.m檔案內容
NYSearchBar.h檔案中面沒有東西,
思路非常easy,就是左邊放一個圖片而已,能夠自己加入其它東東。

////  NYSearchBar.m//  貓貓微博////  Created by apple on 15-7-29.//  Copyright (c) 2015年 znycat. All rights reserved.//#import "NYSearchBar.h"@implementation NYSearchBar- (instancetype)initWithFrame:(CGRect)frame{    if (self = [super initWithFrame:frame]) {        self.font = [UIFont systemFontOfSize:13];        self.background = [UIImage imageWithStretchableName:@"searchbar_textfield_background"];        // 設定左邊的view        // initWithImage:預設UIImageView的尺寸跟圖片一樣        UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchbar_textfield_search_icon"]];        // 為了空出左邊一小塊設定的        imageV.width += 10;        imageV.contentMode = UIViewContentModeCenter;        self.leftView = imageV;        // 一定要設定。想要顯示搜尋方塊左邊的視圖,一定要設定左邊視圖的模式        self.leftViewMode = UITextFieldViewModeAlways;    }    return self;}@end

推薦一個iOS學習帥氣的網站 : code4app

各種各樣的iOS效果和原始碼都用,隨下隨用。

貓貓學iOS 之微博項目實戰(5)微博自己定義搜尋方塊searchBar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.