Cannot change text and background color of searchbar in navigation bar

Ryasoh

I've added a searchbar to my navigation bar and want to change the color of the text and background. I'm using the code below and based on other answers on SO appearanceWhenContainedIn should work but does nothing. What's going wrong here?

UISearchBar * theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,40)]; // frame has no effect.

    theSearchBar.delegate = self;
    if ( !searchBarPlaceHolder ) {
        searchBarPlaceHolder = @"What are you looking for?";
    }
    theSearchBar.placeholder = searchBarPlaceHolder;
    theSearchBar.showsCancelButton = NO;

    [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];

    UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
             initWithSearchBar:theSearchBar
             contentsController:self ];

    [searchCon setActive:NO animated:YES];
    self.searchDisplayController = searchCon;
    self.searchDisplayController.delegate = self;
    self.searchDisplayController.searchResultsDataSource = self;
    self.searchDisplayController.searchResultsDelegate = self;
    self.searchDisplayController.displaysSearchBarInNavigationBar=YES;
Ryasoh

The following code helped me fix the issue. I have read methods that set private API may be rejected from apple, and route to go is subView. See below:

    if(!itemSearchBar)
    {
        itemSearchBar = [[UISearchBar alloc] init];
        [itemSearchBar setFrame:CGRectMake(0, 0, self.view.frame.size.width, 55)];
        itemSearchBar.placeholder = @"What are you looking for?";
        itemSearchBar.delegate = self;


        UITextField* sbTextField;

        for (UIView *subView in self.itemSearchBar.subviews){
            for (UIView *ndLeveSubView in subView.subviews){

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

                    sbTextField = (UITextField *)ndLeveSubView;
                    //changes typing text color
                    sbTextField.textColor = [UIColor redColor];
                    //changes background color
                    sbTextField.backgroundColor = [UIColor blueColor];
                    //changes placeholder color
                            UIColor *color = [UIColor redColor];
                            sbTextField.attributedPlaceholder =
                            [[NSAttributedString alloc]
                             initWithString:@"What are you looking for?"
                             attributes:@{NSForegroundColorAttributeName:color}];

                    break;
                }

            }

        }

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Text element background color change

분류에서Dev

XPages - change background color of place bar button

분류에서Dev

How to change web browser's tab bar background color or image?

분류에서Dev

Navigation Drawer change the Icon and text color of highlighted item

분류에서Dev

Background color change on page load

분류에서Dev

change background color on webpage jQuery

분류에서Dev

Flutter: How to change the status bar text color on Android and iOS when not using AppBar

분류에서Dev

changing text of custom navigation bar item

분류에서Dev

HTML positioning: overlap between navigation bar and text

분류에서Dev

Cannot change color of a-tag

분류에서Dev

Change the text color on WPF Calendar?

분류에서Dev

Change the text color of the popup menu

분류에서Dev

<meter> Color Change on Animation Health Bar

분류에서Dev

Customize iOS 7 status bar text Color

분류에서Dev

Change the background color of a layout from the code

분류에서Dev

How to change a font/background color in transparent windows?

분류에서Dev

UITableView change background color of specific cell

분류에서Dev

The background color of the dropdown list in my navigation wont display properly

분류에서Dev

Change the PS1 color based on the background color?

분류에서Dev

Cannot change login screen background in 18.10

분류에서Dev

How to change the color of the text of a label in Xcode

분류에서Dev

Change the text color of PlaceholderText (Windows 8.1 App)

분류에서Dev

Using jQuery Switch Case to Change text Color

분류에서Dev

Count and change text color of occurrences of string in NSTextView

분류에서Dev

Result of $var ($unread) cause text to change color

분류에서Dev

How to change the color of the text in below code?

분류에서Dev

Change color of text in pop up message box

분류에서Dev

How to change section color of a stacked bar chart in Google Charts API?

분류에서Dev

set text color of Appcompat action bar does not work

Related 관련 기사

  1. 1

    Text element background color change

  2. 2

    XPages - change background color of place bar button

  3. 3

    How to change web browser's tab bar background color or image?

  4. 4

    Navigation Drawer change the Icon and text color of highlighted item

  5. 5

    Background color change on page load

  6. 6

    change background color on webpage jQuery

  7. 7

    Flutter: How to change the status bar text color on Android and iOS when not using AppBar

  8. 8

    changing text of custom navigation bar item

  9. 9

    HTML positioning: overlap between navigation bar and text

  10. 10

    Cannot change color of a-tag

  11. 11

    Change the text color on WPF Calendar?

  12. 12

    Change the text color of the popup menu

  13. 13

    <meter> Color Change on Animation Health Bar

  14. 14

    Customize iOS 7 status bar text Color

  15. 15

    Change the background color of a layout from the code

  16. 16

    How to change a font/background color in transparent windows?

  17. 17

    UITableView change background color of specific cell

  18. 18

    The background color of the dropdown list in my navigation wont display properly

  19. 19

    Change the PS1 color based on the background color?

  20. 20

    Cannot change login screen background in 18.10

  21. 21

    How to change the color of the text of a label in Xcode

  22. 22

    Change the text color of PlaceholderText (Windows 8.1 App)

  23. 23

    Using jQuery Switch Case to Change text Color

  24. 24

    Count and change text color of occurrences of string in NSTextView

  25. 25

    Result of $var ($unread) cause text to change color

  26. 26

    How to change the color of the text in below code?

  27. 27

    Change color of text in pop up message box

  28. 28

    How to change section color of a stacked bar chart in Google Charts API?

  29. 29

    set text color of Appcompat action bar does not work

뜨겁다태그

보관