anon
anon

Reputation:

Can't change the size of UISearchbar's text field

I have a UISearchBar created in InterfaceBuilder and IBOutlet of it called userSearchBar. Now I want to change its width and height in code and can't do it. Here is my code:

    for (UIView * const subview in userSearchBar.subviews) {

    if ([subview isKindOfClass: NSClassFromString(@"UISearchBarBackground")]) {
        [subview removeFromSuperview];
    }

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

        UITextField * const textField = (UITextField *)subview;

        textField.textColor = [UIColor redColor];
        textField.text = @"Enter text";
        textField.frame = CGRectMake(0, 0, 250, 50);
    }
}

All other changes (text, color, removing bar) works perfectly but changing the size not.

Upvotes: 0

Views: 1680

Answers (1)

Ken
Ken

Reputation: 31161

I've come up against exactly this myself, and I found the best way through was to write my own search bar from scratch, subclassing UIView. This gives me legitimate access to all the properties I might want to customise (such as background, colour when considering design or brand), rather than trying hack through like this. It's future proof, not that hard, and not hugely time consuming. It's a fun exercise and will deliver exactly what you want.

Upvotes: 6

Related Questions