Uisearchbar search box components in IOS Basic use guide _ios

Source: Internet
Author: User
Tags set background uikit

Uisearchbar is also one of the most common controls for iOS development, and 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 placeholder color, modify Uisearchbar above Uitextfield background color, modify Uitextfield above photos and so on.

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

LSSearchBar.h is as follows:

Copy Code code as follows:

#import <UIKit/UIKit.h>

@interface Lssearchbar:uisearchbar

@end

LSSEARCHBAR.M is as follows:

Copy Code code as follows:

#import "LSSearchBar.h"

@implementation Lssearchbar

-(void) Layoutsubviews {

[Super Layoutsubviews];

Find Searchfield by traversing Self.subviews
Uitextfield *searchfield;
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 can not 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 up a picture on a Searchfield
UIImage *image = [uiimage imagenamed:@ "search"];
Uiimageview *iview = [[Uiimageview alloc] initwithimage:image];
Iview.frame = CGRectMake (0, 0, 15, 15);
Searchfield.leftview = IView;
}

}

@end

Modify Uisearchbar background color
Isearchbar is made up of two subview, one is Uisearchbarbackground and the other is Uitextfield. To IB there are no properties that directly manipulate the background. The method is to move the uisearchbarbackground directly.

Copy Code code as follows:

Seachbar=[[uisearchbar alloc] init];
Seachbar.backgroundcolor=[uicolor Clearcolor];
For (UIView *subview in seachbar.subviews) {
if ([Subview iskindofclass:nsclassfromstring (@ "Uisearchbarbackground")]) {
[Subview Removefromsuperview];
Break
}
}

Uisearchbar Text color change
1. In iOS 7 access text fields, you have to repeat at the level more. Change your code like this

Copy Code code as follows:

For (UIView *subview in Self.searchBar.subviews)
{
For (UIView *secondlevelsubview in subview.subviews) {
if ([Secondlevelsubview Iskindofclass:[uitextfield class]])
{
Uitextfield *searchbartextfield = (Uitextfield *) Secondlevelsubview;
Set Font color here
Searchbartextfield.textcolor = [Uicolor blackcolor];
Break
}
}
}

Or you can set the Tintcolor to apply to the key in search bar. Use the Tintcolor to shading foreground to use the Bartintcolor bar background you want to color. In the iOS system V7.0, the UIView subclass derives from the base class behavior Tintcolor. See Tintcolor in for UIView level Apple file
2. You can set the color of the text
Copy Code code as follows:

[[Uitextfield Appearancewhencontainedin:[uisearchbar class], nil] settextcolor:[uicolor Bluecolor]];

3. While this is true, the Uiappearance protocol is a "public API," which is not true, Uitextfield supports this. If you look at the UITextField.h and look for the string "Ui_appearance_selector", you will see that it has any instances of this string. If you look at the UIButton codego.net, you will find a lot-these are the attributes that are officially supported by the Uiappearance API. This is well known, Uitextfield is not supported by the Uiappearance API, so the Sandip answer code is not always feasible, and it is not really the best method. This is a link to a post: the right thing to do is to traverse the child view (or child view primarily for IOS7) and manually set it. Otherwise, you will have unreliable results. But you can create a category of Uisearchbar and add SetTextColor: (* uicolor) Example:
Copy Code code as follows:

-(void) SetTextColor: (uicolor*) color
{
For (UIView *v in Self.subviews)
{
if ([Environment Isversion7orhigher])//checks uidevice#systemversion
{
For (ID subview in v.subviews)
{
if ([Subview Iskindofclass:[uitextfield class]])
{
((Uitextfield *) subview). TextColor = color;
}
}
}
Else
{
if ([V Iskindofclass:[uitextfield class]])
{
((Uitextfield *) v). TextColor = color;
}
}
}
}

Customizing the Uisearchbar background image

Copy Code code as follows:

-(void) Layoutsubviews {
Uitextfield *searchfield;
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 (!) ( Searchfield = = nil)) {
Searchfield.textcolor = [Uicolor Whitecolor];
[Searchfield.leftview Sethidden:yes];
[Searchfield setbackground: [uiimage imagenamed:@ "Searchbarbackground.png"]];
[Searchfield Setborderstyle:uitextborderstylenone];
}

[Super Layoutsubviews];
}

Related Article

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.