簡體   English   中英

UISearchBar中范圍按鈕文本的顏色

[英]Color of scope button text in UISearchBar

我有一個UISearchBar,它必須有灰色調。

我現在遇到的問題是,一旦我設置了任何色調顏色,范圍按鈕就會得到相同的字體顏色,如果沒有選擇按鈕,會導致對比度非常差。

如果未設置色調,則顏色會根據所選狀態而有所不同。 有沒有辦法實現這一點,使用淡色?

默認

帶着色調

使用色調

沒有色彩

更新

使用po [mySearchBar recursiveDescription]我發現UISearchbar具有以下視圖層次結構:

<UISegmentedControl>
    <_UISegmentedControlBackgroundView>
        <UIImageView>
    <UISegment>
        <UISegmentLabel>
        <UIImageView>
    <UISegment>
        <UISegmentLabel>
        <UIImageView>
    <UISegment>
        <UISegmentLabel>
        <UIImageView>
<UISearchBarBackground>

我似乎記得在使用色調時會遇到類似的問題。 看起來不幸的副作用是它默認與其影響的UIKit元素的顏色相關屬性。

我不知道在使用色調時是否有辦法防止此缺陷(我認為沒有),但以下解決方法可能對您有用:

iOS 6及以下版本

[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], UITextAttributeTextColor, nil] forState:UIControlStateSelected];

iOS 7+

[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];

(信用:@willyang)

使用UISearchBarsetScopeBarButtonTitleTextAttributes:forState:方法,您可以配置范圍欄按鈕標題的屬性,包括文本顏色。

由於UITextAttributeTextColor在IOS 7過時,應該用NSForegroundColorAttributeName改為:

[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
[searchBar setScopeBarButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];

參考: http//www.downstairz.net/2013/11/24/uitextattributetextcolor-is-deprecated-in-ios-7/

斯威夫特3:

如果以編程方式構建SearchBar,可以使用以下命令設置ScopeBar的TextAttributes:

let searchController = UISearchController(searchResultsController: nil) // inside the class

....

// Background color
        searchController.searchBar.setScopeBarButtonTitleTextAttributes([NSBackgroundColorAttributeName: UIColor.yellow ], for: UIControlState.normal)

您可以查看UISearchBar方法(向下滾動以查找ScopeBar方法)進行自定義: https//developer.apple.com/reference/uikit/uisearchbar? language = objc

如果我正確地提出了您的問題,那么這就是您要做的事情

for (id subview in yourSearchDisplayController.searchBar.subviews )
{

    if([subview isMemberOfClass:[UISegmentedControl class]])
    {

        UISegmentedControl *scopeBar=(UISegmentedControl *) subview;
        [scopeBar setSegmentedControlStyle:UISegmentedControlStyleBar];
        [scopeBar setTintColor: [UIColor redColor]];//you can also set RGB color here 
        //scopeBar.tintColor =  [UIColor blackColor];         
    }

}

暫無
暫無

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

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