How can I change a UITextField position in UISearchBar?
Asked Answered
L

3

11

Is there a way to reposition UITextField inside UISearchBar? I'd like to move UITextField inside UISearchBar up a little bit.

Lindstrom answered 22/7, 2011 at 15:42 Comment(1)
I think you have to implement your own search bar.Judy
P
14

The UITextField approach is not working anymore, probably due to a restyling of the UISearchBar object. UISearchBar resizes its UITextField when it is presented on screen, this invalidates all manipulations on the UITextField.

However, I discovered that UISearchBar responds to the setContentInset: method (tested on iOS 5.0.1). It adjusts the inset distance from the enclosing view.

This result in a sophisticated approach involving NSInvocation:

if([aSearchBar respondsToSelector:@selector(setContentInset:)])
{
    SEL aSelector = NSSelectorFromString(@"setContentInset:");
    NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[aSearchBar methodSignatureForSelector:aSelector]];

    [inv setSelector:aSelector];
    [inv setTarget:aSearchBar];
    UIEdgeInsets anInsets = UIEdgeInsetsMake(5, 0, 5, 35);
    [inv setArgument:&anInsets atIndex:2]; //arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation
    [inv invoke];
}

The above code resizes the contentInset of the UISearchBar (effectively UITextField) using the CGRect indicated in the anInsets parameter. The parent if-statement (respondsToSelector:) saves yourself from changes of this behaviour in new versions of iOS.

Updated for Swift3 and iOS10:

if searchBar.responds(to: Selector("setContentInset:")) {
   let aSelector = Selector("setContentInset:")
   let anInset = UIEdgeInsetsMake(5, 0, 5, 35)
   searchBar.perform(aSelector, with: anInset)
}
Plattdeutsch answered 16/1, 2012 at 9:47 Comment(8)
Works like a charm. For example, to have some space left and right, use the above with: UIEdgeInsets anInsets = UIEdgeInsetsMake(5, 50 /* left */, 5, 50 /* right */);Veolaver
I found that if you leave the top and bottom insets as 0, it applies the default inset. Set as 5, a but happened when I selected the text field that caused it to move up to the top of the searchbar.Feints
Will this code reject your app from AppStore? Other than that, it does, indeed, work like a charmOpaline
No use of private APIs here and The user interface experience is not changed. So, i think that you can safely submit it to the AppStore.Plattdeutsch
This will reject APIGrasshopper
@Grasshopper as far as I know this option is not an issue for the AppStore. Best.Plattdeutsch
I am using searchbar in navigation and I want to move only the text to right (say by 50p) . This code moves the complete search bar. Is there a way to keep the search icon as it is but move the text ?Spermatophore
@Spermatophore Probably too late for an answer but you can do that with property searchTextPositionAdjustmentSpermatophore
C
12

Swift 4+

You can try this.

searchBar.searchTextPositionAdjustment = UIOffset(horizontal: 5, vertical: 0)

It doesn't move the textField, but moves the text. This should be enough for most people.

Cretin answered 12/8, 2018 at 5:23 Comment(0)
H
0

The best solution is changing of directionalLayoutMargins

searchBar.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 0.0, leading: 20.0, bottom: 0.0, trailing: 20.0)
Headreach answered 31/1, 2024 at 17:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.