On the Uisearchbar of iOS development

Source: Internet
Author: User
Tags set background uikit

On the Uisearchbar of iOS development

Uisearchbar is also one of the most common iOS development controls, click inside to see the properties Barstyle, text, placeholder, and so on. But these attributes are clearly inadequate to meet our development needs. For example: Modify the color of the placeholder, modify the Uisearchbar above the Uitextfield background color, modify Uitextfield above the photos and so on.

In order to achieve the above requirements, it is best to write a uisearchbar subclass called Lssearchbar Bar

LSSearchBar.h as follows:

#import <UIKit/UIKit.h>@interface LSSearchBar : UISearchBar@end

LSSEARCHBAR.M as follows:

#import "LSSearchBar.h" @implementation lssearchbar-(void) layoutsubviews {[Super layoutsubviews];    Find Searchfield Uitextfield *searchfield by traversing self.subviews;    Nsuinteger numviews = [self.subviews count];            for (int i = 0; i < numviews; i++) {if ([[[Self.subviews objectatindex:i] Iskindofclass:[uitextfield class]) {        Searchfield = [Self.subviews objectatindex:i];        }}//If the above method cannot find Searchfield, then try the following method if (Searchfield = = nil) {Nsarray *arraysub = [self subviews];        UIView *viewself = [Arraysub objectatindex:0];        Nsarray *arrayview = [Viewself subviews]; for (int i = 0; i < Arrayview.count; i++) {if ([[[Arrayview objectatindex:i] Iskindofclass:[uitextfield class]            ]) {Searchfield = [Arrayview objectatindex:i]; }}} if (! (        Searchfield = = nil) {//Set color Searchfield.textcolor = [Uicolor Whitecolor]; Set background color [Searchfield setbackground: [UIImage imagenamed:@ "Searchbar"];        [Searchfield Setborderstyle:uitextborderstylenone];        Set the color of the placeholder [Searchfield setvalue:[uicolor Whitecolor] forkeypath:@ "_placeholderlabel.textcolor"];        Set the photo on Searchfield UIImage *image = [UIImage imagenamed:@ "search"];        Uiimageview *iview = [[Uiimageview alloc] initwithimage:image];        Iview.frame = CGRectMake (0, 0, 15, 15);    Searchfield.leftview = IView; }} @end

The source of this article just online: http://www.superqq.com/blog/2015/01/19/ioskai-fa-zhi-uisearchbarchu-tan/

On the Uisearchbar of iOS development

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.