zoukankan      html  css  js  c++  java
  • 修改UISearBar的文字颜色,placehoder颜色及输入框颜色

    UISearchBar是我们经常会用到的一个控件~~

    它由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField

    UITextField默认输入字体是黑色的,当背景是黑色时,输入的内容就会看不到,如下图:

    UISearBar有两个控制颜色的属性:

    @property(nonatomic,retain) UIColor *tintColor;

    设置这个颜色值会影响搜索框中的光标的颜色

    @property(nonatomic,retain) UIColor *barTintColor;

    设置这个颜色会影响搜索框的背景颜色

    它们没法改变输入文字的颜色,那要如何改变呢,需要循环找到UITextFiled,然后更改:

    for (UIView* subview in [[_searchBar.subviews lastObject] subviews]) {
            
            if ([subview isKindOfClass:[UITextField class]]) {
                UITextField *textField = (UITextField*)subview;
                
                textField.textColor = [UIColor whiteColor];                //修改输入字体的颜色
                [textField setBackgroundColor:[UIColor grayColor]];      //修改输入框的颜色
                [textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];//修改placeholder的颜色
            } else if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
                [subview removeFromSuperview];
            }
      }

    然后效果如下:

    PS:具体的颜色修改根据自己项目需要自行修改!

  • 相关阅读:
    盘子序列
    最大矩形面积
    【模板】ST表
    排队
    map循环遍历
    vue循环遍历给div添加id
    正则 匹配
    字符串拼接
    js对象追加到数组里
    二级标题左侧加粗线条
  • 原文地址:https://www.cnblogs.com/china-fanny/p/6222669.html
Copyright © 2011-2022 走看看