你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS中的UISearchBar搜索框組件基礎使用指南

iOS中的UISearchBar搜索框組件基礎使用指南

編輯:IOS開發綜合

UISearchBar也是iOS開發常用控件之一,點進去看看裡面的屬性barStyle、text、placeholder等等。但是這些屬性顯然不足矣滿足我們的開發需求。比如:修改placeholder的顏色、修改UISearchBar上面的UITextfield的背景顏色、修改UITextfield上面的照片等等。

為了實現上述的需求,最好寫一個UISearchBar的子類就叫LSSearchBar吧

LSSearchBar.h如下:
復制代碼 代碼如下:
#import <UIKit/UIKit.h>

@interface LSSearchBar : UISearchBar

@end

LSSearchBar.m如下:
復制代碼 代碼如下:
#import "LSSearchBar.h"

@implementation LSSearchBar

- (void)layoutSubviews {

    [super layoutSubviews];

    //通過遍歷self.subviews找到searchField
    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];
        }
    }

    //如果上述方法找不到searchField,那就試試下面的方法吧

    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)) {
        //設置顏色
        searchField.textColor = [UIColor whiteColor];

        //設置背景顏色
        [searchField setBackground: [UIImage imageNamed:@"searchbar"] ];
        [searchField setBorderStyle:UITextBorderStyleNone];

        //設置placeholder的顏色
        [searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];

        //設置searchField上的照片
        UIImage *image = [UIImage imageNamed:@"search"];
        UIImageView *iView = [[UIImageView alloc] initWithImage:image];
        iView.frame = CGRectMake(0, 0, 15, 15);
        searchField.leftView = iView;
    }

}

@end

修改UISearchBar背景顏色
ISearchBar是由兩個subView組成的,一個是UISearchBarBackGround,另一個是UITextField. 要IB中沒有直接操作背景的屬性。方法是直接將 UISearchBarBackGround移去 
復制代碼 代碼如下:
seachBar=[[UISearchBar alloc] init]; 
seachBar.backgroundColor=[UIColor clearColor]; 
for (UIView *subview in seachBar.subviews){   
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])  {   
        [subview removeFromSuperview];   
    break; 
    }  
}

UISearchBar文字顏色改變
1. 在iOS的7訪問文本字段,你必須在水平重申更多。更改您的代碼像這樣
復制代碼 代碼如下:
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;
  }
 }
}

或可以設置的tintcolor適用於關鍵在search bar。 使用tintColor至著色前景 使用barTintColor要著色的欄背景。 在iOS系統V7.0,的UIView的子類派生的基類行為tintColor。見tintColor在為UIView的水平 蘋果文件
2. 可以通過設置文字的顏色
復制代碼 代碼如下:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];

3. 雖然這是真的,UIAppearance協議是一個“公開的API,”這不是真的,UITextField的支持這一點。 如果你看一看UITextField.h並查找字符串“UI_APPEARANCE_SELECTOR”,你會看到它有這個字符串的任何實例。如果你看的UIButton CodeGo.net,你會發現不少-這些都是由該UIAppearance API正式支持的屬性。這是眾所周知的,UITextField的是不支持的UIAppearance API,所以在桑迪普的答案代碼並不總是可行的,它實際上不是最好的方法。 這是帖子的鏈接: 正確的做法是-遍歷子視圖(或子視圖主要用於IOS7的子視圖)和手動設置。否則,您將有不可靠的結果。但你可以創建一個類別的UISearchBar並添加setTextColor:(*的UIColor)示例:
復制代碼 代碼如下:
- (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;
   }
  }
 }
}

自定義UISearchBar的背景圖
復制代碼 代碼如下:
- (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];
}

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved