Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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. </p> <p>Here's the code:</p> <p>For creating the UISearchBar in MyRootViewController</p> <pre><code>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]; </code></pre> <p>UITextFieldDelegate in MyRootViewController (make sure that it implements the UITextFieldDelegate protocol)</p> <pre><code>- (void)textFieldDidBeginEditing:(UITextField *)textField { [self.searchBar setCloseButtonFont:[UIFont fontWithName:@"American Typewriter" size:14] textColor:[UIColor grayColor]]; } </code></pre> <p>And this is the public method in the UISearchBar's subclass</p> <pre><code>- (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]; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload