On iOS 7 UISearchBar the placeholder text is centred and I want to disable that and make it always stick to the left like it was before.
I haven't tried it with a UISearchBar
but this works with a UITextField
. I Subclassed it and overrode leftViewRectForBounds:
, textRectForBounds:
and editingRectForBounds:
, here is the code:
- (CGRect)textRectForBounds:(CGRect)bounds {
CGRect inset = CGRectMake(bounds.origin.x + kLeftTextPadding,
bounds.origin.y,
bounds.size.width - kLeftTextPadding,
bounds.size.height);
return inset;
}
Hope it helps.
To get left alignment of the placeholder in iOS7 you can add white spaces to the end of your placeholder string. For example:
_searchBar.placeholder = @"Search ";
Note that it is should be done separately for each search bar and each language.
One could try to write automatic calculation of needed spaces after text for any language but it seems impossible to read the size of searchBar's textField frame
<UISearchBarTextField: 0x1349c160; frame = (0 0; 0 0);