日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UISearchBar 自定义处理

發(fā)布時(shí)間:2024/8/24 编程问答 27 如意码农
生活随笔 收集整理的這篇文章主要介紹了 UISearchBar 自定义处理 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

首先通過 KVC 獲取到內(nèi)部的 textField, 然后自定制處理

UITextField *searchField = [searchBar valueForKey:@"searchField"];

if (searchField) {

[searchField setBorderStyle:UITextBorderStyleNone];

//placeHolder顏色

[searchField setValue:_placeholderColor forKeyPath:@"_placeholderLabel.textColor"];

searchField.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);

searchField.textColor = _textColor;

//字體大小

[searchField setValue:_placeHolderFont forKeyPath:@"_placeholderLabel.font"];

searchField.font = _textFont;

UIImage *image = _leftImage;

UIImageView *leftImg = [[UIImageView alloc] initWithImage:image];

leftImg.frame = CGRectMake(0,0,image.size.width, image.size.height);

searchField.leftView = leftImg;

}

其他: 添加圓角(放在一個(gè)有圓角的 bgView上面)

UIView *bg = [[UIView alloc]initWithFrame:CGRectMake(20, 220, 300, 30)];

bg.backgroundColor = [UIColor whiteColor];

[self.view addSubview:bg];

bg.layer.cornerRadius = 15;

bg.layer.masksToBounds = YES;

UISearchBar *bar = [[UISearchBar alloc]initWithFrame: bg.bounds];

[bg addSubview:bar];

bar.placeholder = @"搜索...";

[bar setBackgroundImage:[UIImage imageNamed:@"clearImage@2x"]];

修改圓角大小

首先在 UIView 的 category 里加一個(gè)方法:
- (UIView*)subViewOfClassName:(NSString*)className {
for (UIView* subView in self.subviews) {
if ([NSStringFromClass(subView.class) isEqualToString:className]) {
return subView;
} UIView* resultFound = [subView subViewOfClassName:className];
if (resultFound) {
return resultFound;
}
}
return nil;
} 用的時(shí)候:
UIView* backgroundView = [searchBar subViewOfClassName:@"_UISearchBarSearchFieldBackgroundView"];
backgroundView.layer.cornerRadius = 14.0f;
backgroundView.clipsToBounds = YES;
用這個(gè)方法還可以改取消按鈕的顏色、字體什么的。

總結(jié)

以上是生活随笔為你收集整理的UISearchBar 自定义处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。