Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar search icon isnot left aligned in iOS7

In iOS7 the search icon and place holder text are always appearing in middle.I tried changing the text alignment of the text field in search bar to left but it didn't work. Is there any way to make the searchbar search icon appear left aligned in iOS7

I tried the following lines but it did not work:

UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
txfSearchField.textAlignment = NSTextAlignmentLeft;

[_searchArticle setImage:image forSearchBarIcon:UISearchBarIconSearch    state:UIControlStateNormal];
UIimageView *im = [UIImageView alloc]init];
im.image = image;
txfSearchField.leftView =im;
like image 625
user1907370 Avatar asked Oct 10 '13 07:10

user1907370


1 Answers

There is no way to change the alignment of search bar's placeholder text, I tried so many "SO" answer but no one was working. Finally I ended with this workaround

Give some white space in right of placeholder text

    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
        // Load resources for iOS 6.1 or earlier
        self.searchBar.placeholder = @"hello";
   } else {
       // Load resources for iOS 7 or later
       self.searchBar.placeholder = @"hello             ";
  }

Enjoy !!

like image 117
kmithi Avatar answered Oct 05 '22 22:10

kmithi