美文网首页
iOS原生搜索框样式的简单自定义

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

作者: 夜听风雨雨不语 | 来源:发表于2019-07-06 00:57 被阅读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。

相关文章

网友评论

      本文标题:iOS原生搜索框样式的简单自定义

      本文链接:https://www.haomeiwen.com/subject/bnkuhctx.html