Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6550717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:15:00+00:00 2026-05-25T12:15:00+00:00

I have a UISearchBar as seen below. How can I change the text color

  • 0

I have a UISearchBar as seen below. How can I change the text color for the cancel button?

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:15:01+00:00Added an answer on May 25, 2026 at 12:15 pm

    This question was asked a while ago, therefore I assume that the person who asked has already found a solution. But just in case some others happen to bump into the same problem. Here is my solution.

    I have a UISearchBar with a cancel button that appears only when the textfield of the UISearchBar is tapped. The solution of overriding -(void)layoutSubviews in a subclass of UISearchBar was therefore not an option for me. Anyway I did a subclass of UISearchBar (CustomSearchBar) with a public method for setting the font and textColor of the cancel button. When I create the UISearchBar I make sure that the search bar’s textfield delegate is set to self and the class that creates the search bar, implements the UITextFieldDelegate protocol. When the user taps on the search bar’s textfield, its delegate is informed and calls the method of CustomSearchBar. The reason why I do it here, is because it’s the moment when the cancel button appears and therefore I know that it is on the view hierarchy and I can do its customization.

    Here’s the code:

    For creating the UISearchBar in MyRootViewController

    CustomSearchBar *searchBar = [[CustomSearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40)];
    [searchBar setBarStyle:UIBarStyleDefault];
    [searchBar setTintColor:[UIColor whiteColor]];
    
    for (UIView *view in [searchBar subviews]) 
    {
        if ([view isKindOfClass:[UITextField class]]) 
        {
            UITextField *searchTextField = (UITextField *)view;
            [searchTextField setDelegate:self];
        }
    }
    
    self.searchBar = searchBar;   
    [searchBar release];
    

    UITextFieldDelegate in MyRootViewController (make sure that it implements the UITextFieldDelegate protocol)

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        [self.searchBar setCloseButtonFont:[UIFont fontWithName:@"American Typewriter" size:14] textColor:[UIColor grayColor]];
    }
    

    And this is the public method in the UISearchBar’s subclass

    - (void)setCloseButtonFont:(UIFont *)font textColor:(UIColor *)textColor
    {
        UIButton *cancelButton = nil;
    
        for(UIView *subView in self.subviews)
        {
            if([subView isKindOfClass:[UIButton class]])
            {
                cancelButton = (UIButton*)subView;
            }
        }
    
        if (cancelButton)
        {
            /* For some strange reason, this code changes the font but not the text color. I assume some other internal customizations      make this not possible:
    
            UILabel *titleLabel = [cancelButton titleLabel];
            [titleLabel setFont:font];
            [titleLabel setTextColor:[UIColor redColor]];*/
    
            // Therefore I had to create view with a label on top:        
            UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(2, 2, kCancelButtonWidth, kCancelButtonLabelHeight)];
            [overlay setBackgroundColor:[UIColor whiteColor]];
            [overlay setUserInteractionEnabled:NO]; // This is important for the cancel button to work
            [cancelButton addSubview:overlay];
    
            UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, kCancelButtonWidth, kCancelButtonLabelHeight)];
            [newLabel setFont:font];
            [newLabel setTextColor:textColor];
            // Text "Cancel" should be localized for other languages
            [newLabel setText:@"Cancel"]; 
            [newLabel setTextAlignment:UITextAlignmentCenter];
            // This is important for the cancel button to work
            [newLabel setUserInteractionEnabled:NO]; 
            [overlay addSubview:newLabel];
            [newLabel release];
            [overlay release]; 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using iOS and a UISearchBar you can have a nice little cancel button setup
I have a UISearchBar. I enabled the 'cancel' button so it says cancel right
I have a simple UISearchBar with a cancel button. When I call the following:
I have a UISearchBar that has a cancel button (it's displayed using -(void)setShowsCancelButton:animated ).
I have a subclass of UITableViewController. I have code that can add/remove a UISearchBar
I have a UISearchBar which is subviewed by another view when a button is
I have a UISearchBar below a header-view. This header is 64px hight. If the
The UINavigationBar and UISearchBar both have a tintColor property that allows you to change
Given a UITableViewController with a UISearchBar, how does one change the 'No Results' text
I have uisearchbar in a uiview which is already subview of a uiviewController, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.