iOS原生搜索框样式的简单自定义

2019-07-06  本文已影响0人  夜听风雨雨不语

原生搜索框的结构并不复杂,可以通过subViews属性找到对应的view,更改背景色、边框等属性。

简约搜索框

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(70, 5, YK_SCREEN_WIDTH - 90, 50)];

searchBar.tintColor = UIColor.redColor; //光标颜色

searchBar.barTintColor = UIColor.whiteColor;

searchBar.delegate = self;

UIView *subView = searchBar.subviews.firstObject;

for(id temp in subView.subviews) {

if ([temp isKindOfClass:[UITextField class]]) {

UITextField *textField = (UITextField *)temp;

textField.backgroundColor = [UIColor colorWithHexString:@"0xf5f5f5" withAlpha:1.0f];//输入框背景色

}

if ([temp isKindOfClass:[UIImageView class]]) {

UIImageView *imageView = (UIImageView *)temp;

imageView.layer.borderColor = UIColor.whiteColor.CGColor;//可以试试不加这个是什么样的

imageView.layer.borderWidth=1.f;

}

}

对于更自定义要求更高的搜索框可以自己写一个,网上有很多Demo。

上一篇 下一篇

猜你喜欢

热点阅读