標籤:推薦 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