国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS中的UISearchBar搜索框組件基礎(chǔ)使用指南

2019-10-21 18:54:13
字體:
供稿:網(wǎng)友
iOS開發(fā)套件中自帶的UISearchBar搜索框我們平時(shí)經(jīng)常可以用到,我們可以在默認(rèn)的基礎(chǔ)上修改文字顏色、背景顏色和背景圖片等,這里我們稍微總結(jié)一下iOS中的UISearchBar搜索框組件基礎(chǔ)使用指南.
 

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

為了實(shí)現(xiàn)上述的需求,最好寫一個(gè)UISearchBar的子類就叫LSSearchBar吧

LSSearchBar.h如下:

復(fù)制代碼代碼如下:

#import <UIKit/UIKit.h>

 

@interface LSSearchBar : UISearchBar

@end

 

LSSearchBar.m如下:

復(fù)制代碼代碼如下:

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

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

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

        //設(shè)置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是由兩個(gè)subView組成的,一個(gè)是UISearchBarBackGround,另一個(gè)是UITextField. 要IB中沒有直接操作背景的屬性。方法是直接將 UISearchBarBackGround移去  

復(fù)制代碼代碼如下:

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訪問文本字段,你必須在水平重申更多。更改您的代碼像這樣

復(fù)制代碼代碼如下:

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;
  }
 }
}

或可以設(shè)置的tintcolor適用于關(guān)鍵在search bar。 使用tintColor至著色前景 使用barTintColor要著色的欄背景。 在iOS系統(tǒng)V7.0,的UIView的子類派生的基類行為tintColor。見tintColor在為UIView的水平 蘋果文件 
2. 可以通過設(shè)置文字的顏色
復(fù)制代碼代碼如下:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];

3. 雖然這是真的,UIAppearance協(xié)議是一個(gè)“公開的API,”這不是真的,UITextField的支持這一點(diǎn)。 如果你看一看UITextField.h并查找字符串“UI_APPEARANCE_SELECTOR”,你會(huì)看到它有這個(gè)字符串的任何實(shí)例。如果你看的UIButton CodeGo.net,你會(huì)發(fā)現(xiàn)不少-這些都是由該UIAppearance API正式支持的屬性。這是眾所周知的,UITextField的是不支持的UIAppearance API,所以在桑迪普的答案代碼并不總是可行的,它實(shí)際上不是最好的方法。 這是帖子的鏈接: 正確的做法是-遍歷子視圖(或子視圖主要用于IOS7的子視圖)和手動(dòng)設(shè)置。否則,您將有不可靠的結(jié)果。但你可以創(chuàng)建一個(gè)類別的UISearchBar并添加setTextColor:(*的UIColor)示例:
復(fù)制代碼代碼如下:

- (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的背景圖

復(fù)制代碼代碼如下:

- (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];
}
 


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东乡族自治县| 同德县| 张家界市| 麻江县| 临邑县| 香格里拉县| 西乌珠穆沁旗| 深州市| 关岭| 江山市| 富源县| 富锦市| 安阳县| 辰溪县| 黄陵县| 顺义区| 六安市| 荆门市| 丰顺县| 界首市| 佳木斯市| 长春市| 广平县| 阜阳市| 峡江县| 正宁县| 通化县| 巫溪县| 弥勒县| 堆龙德庆县| 汤阴县| 凌海市| 谢通门县| 平果县| 南充市| 余庆县| 鄂托克旗| 北流市| 兴安县| 台湾省| 嘉善县|