iOS Development--ui OC Chapter &textfield as a search box use

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.