簡體   English   中英

TextField文字搜索欄中的顏色更改

[英]TextField text Color change in searchbar

請檢查下面的代碼,我使用了一個包含搜索searchbar navigationbar searchbar並且搜索searchbar具有文本字段,現在我想更改文本textfield的文本顏色,我做了下面的代碼,但是它不起作用,因此請幫助糾正它。

[[UISearchBar appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]]setTintColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]]setTextColor:[UIColor whiteColor]];

如果您使用的是本機UISearchbar控件,則可以使用以下代碼更改文本顏色

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];

或者,您可以通過Identity Inspector對特定的searchBar執行此操作: 在此處輸入圖片說明

嘗試這個,

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];

for (UIView *view in searchBarSubViews) {
    if([view isKindOfClass:[UITextField class]])
    {
        UITextField *txt = (UITextField*)view;
        UIImageView *imgView = (UIImageView*)textField.leftView;
        imgView.image = [imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        imgView.tintColor = [UIColor whiteColor];

        txt.backgroundColor = [UIColor blackColor];
        txt.textColor = [UIColor RedColor];
        UIButton *btnClear = (UIButton*)[textField valueForKey:@"clearButton"];
        [btnClear setImage:[btnClear.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
        btnClear.tintColor = [UIColor whiteColor];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM