Use of TextField as a search box
In iOS development we often use the search box, but sometimes the system comes with a search box that is not enough to meet my needs, this time we can use a custom search box to achieve the desired function.
Today is a simple introduction to how to use Uitextfield to implement a search box, of course, if you have a better way to share, we learn from each other.
One: Direct use
1Uitextfield *text =[[Uitextfield alloc] init];2Text.frame = CGRectMake (0,0, the, -);3Text.background = [UIImage resizeimage:@"Searchbar_textfield_background"];4 //text is centered vertically5Text.contentverticalalignment =Uicontrolcontentverticalalignmentcenter;6 //Search Icon7Uiimageview *view =[[Uiimageview alloc] init];8View.image = [UIImage resizeimage:@"Searchbar_textfield_search_icon"];9View.frame = CGRectMake (0,0, *, *);Ten //the mode of the left search icon OneView.contentmode =Uiviewcontentmodecenter; AText.leftview =view; - //The left search icon is always displayed -Text.leftviewmode =uitextfieldviewmodealways; the //Delete all icons on the right -Text.clearbuttonmode =uitextfieldviewmodealways; - -Self.navigationItem.titleView = text;
Two: Package
1 //Initialize2 /**3 * Use TextField to implement search box function encapsulation4 */5 //Set Font size6Self.font = [Uifont systemfontofsize: the];7 //Set Prompt Text8Self.placeholder =@"Please enter search criteria";9 //set the background picture of a text box (use categories to achieve stretch effects)TenSelf.background = [UIImage resizeimage:@"Searchbar_textfield_background"]; One //text is centered vertically ASelf.contentverticalalignment =Uicontrolcontentverticalalignmentcenter; - //Search Icon -Uiimageview *view =[[Uiimageview alloc] init]; the //to set a picture of a picture controller -View.image = [UIImage resizeimage:@"Searchbar_textfield_search_icon"]; - //set the frame of the picture -View.width = -; +View.height = -; - //the mode of the left search icon +View.contentmode =Uiviewcontentmodecenter; ASelf.leftview =view; at //The left search icon is always displayed -Self.leftviewmode =uitextfieldviewmodealways; - //Delete all icons on the right -Self.clearbuttonmode = Uitextfieldviewmodealways;
/** Use one:
* Use the encapsulated Searchbar
*/
1 Icocossearchbar *searchbar = [Icocossearchbar searchbar]; 2 - ; 3 - ; 4 Self.navigationItem.titleView = Searchbar;
/** use two
* Use the encapsulated Searchbar
*/
1 Icocossearchbar *searchbar = [Icocossearchbar searchbar]; 2 - ; 3 - ; 4 [Self.view Addsubview:searchbar];
iOS Development--ui OC Chapter &textfield as a search box use