user4951
user4951

Reputation: 33130

Why changing the PlaceHolder to the left doesn't work with this method?

I tried, in IOS 7 to subclass an UISearchBar so that the place holder is always left aligned.

I did this:

- (void)layoutSubviews {
    [super layoutSubviews];
    UITextField * tv = self.textField;

    tv.textAlignment = NSTextAlignmentLeft  ;
}

If tv.textAlignment = NSTextAlignmentRight, then I manage to make the text to the right.

However, the text when UISearchBar is empty, and display the placeholder, always shows to the center.

I wonder why. I put it in layoutSubviews method. Hence, it should be drawn every time the control is drawn.

Upvotes: 0

Views: 1334

Answers (3)

simalone
simalone

Reputation: 2768

Through NSLog the searchBar's textField subviews, can get such a result:

<_UISearchBarSearchFieldBackgroundView: 0x8d492e0; frame = (0 0; 304 28); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x8d49410>> - (null),
<UIImageView: 0x8d488a0; frame = (102.5 7.5; 12.5 12.5); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8d48f80>> - (null),
<UISearchBarTextFieldLabel: 0x8d4b410; frame = (122.5 1; 181.5 25); text = 'hello000000'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8d4b520>>
)

you can see, UISearchBarTextFieldLabel is the UI to show placeholder, here is 'hello000000', which is a subclass of UILabel. Therefore, set searchBar's textField textAlignment will not affect placeholder position directly. And the layout of placeholder label is handled by searchBar's textField - (void)layoutSubviews , whereas you can't override inner textField this method.

A custom searchBar base on UIView, add your subclass of UITextField which override - (void)layoutSubviews, Or even add add UILabel where textfield's text is empty, remove while not. maybe a solution.

Add some my test code:

@interface CustomSearchBar : UIView {
}
@end

@implementation SharedSearchBar
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {        
        _textField = [[SearchTextField alloc] initWithFrame:CGRectMake(10, 6, frame.size.width-20, frame.size.height-12)];
        _textField.leftView = UIImageViewNamed(@"search_glass");
        [self addSubview:_textField];
    }
    return self;
}
@end

@interface SearchTextField : UITextField

@end

@implementation SearchTextField
- (void)layoutSubviews{
    [super layoutSubviews];
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:[UILabel class]]) {
            UILabel *lb = (UILabel *)subView;
            lb.textAlignment = UITextAlignmentRight;
            lb.text = @"123123";
            CGRect frame = lb.frame;
            frame.origin.x = textFiled.frame.size.width-frame.size.width;
            lb.frame = frame;
            break;
        }
    }
}
@end

This is empty result:

Upvotes: 0

user2289379
user2289379

Reputation:

I have same issue before some days ago, but there is no way to change the alignment of search bar's placeholder text, I tested with so many "SO" answer but no one was working. At last I decided with following Fixing.

Add some white space in Left/right (as you want) of placeholder text

if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.1) {
    // Load resources for iOS 6.1 or earlier
    self.searchBar.placeholder = @"hello";
}
 else 
{
    // Load resources for iOS 7 or later
    // Add some white space in Left/right *(as you want)* here i added to left
    self.searchBar.placeholder = @"           hello";
}

Upvotes: 0

Balram Tiwari
Balram Tiwari

Reputation: 5667

Can you try this using UIAppearance.

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextAlignment:NSTextAlignmentLeft];

Downvoters, please read this : note from Apple:

Note: iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.

Upvotes: 1

Related Questions