8
votes

In iOS 7 UISearchbar placeholder center-aligned and overlay the bookmarks button until search bar doesn't selected:

enter image description here

When it selected, it looks as expected:

enter image description here

I need it looks this way all the time. Thank you.

3
Please share some code. Also, make sure to file a bug report with Apple.Leo Natan
you have need any font sizecodercat
what is your uisearchbar frame sizecodercat
it's nothing to do with the frame size ... i guess it's a bug in iOS7's search barJAHelia

3 Answers

3
votes

NEW SOLUTION:

//
//  WPViewController.m
//  test
//
//  Created by VASANTH K on 02/01/14.
// 
//

    #import "WPViewController.h"

    @interface WPViewController ()
    {
        UILabel *lableCopy;
    }

    @end

    @implementation WPViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        //[self fixSearchBar:searchBar];
        // Do any additional setup after loading the view, typically from a nib.

        self.searchBar.delegate=self;
    }

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {

        [self.searchBar resignFirstResponder];
        //[self fixSearchBar:searchBar];
    }
    -(void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];

        [self fixSearchBar:self.searchBar];

    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }


    -(void)searchBarTextDidBeginEditing:(UISearchBar *)search
    {
        [self fixSearchBar:self.searchBar];
    }

    -(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
    {
         [self fixSearchBar:self.searchBar];
    }

    -(void)fixSearchBar:(UISearchBar*)searchBar
    {
        UITextField *searchField = [searchBar valueForKey:@"_searchField"];

        // [searchField setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"];

        UILabel *lable=[searchField valueForKey:@"_placeholderLabel"];

        if(!lableCopy)
        {
            lableCopy=[[UILabel alloc]initWithFrame:lable.frame];
            lableCopy.font=lable.font;
            [lableCopy setText:lable.text];
            [lableCopy setTextColor:lable.textColor];
            UIButton *button;

            for (UIView *view in [[[[searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:1] subviews]) {
                if([view isKindOfClass:[UIButton class]])
                {
                    button=(UIButton*)view;
                    break;
                }
            }



            if(button)
            {
                //lable.hidden=YES;
                CGRect newFrame=lable.frame;
                newFrame.size.width=button.frame.origin.x-lable.frame.origin.x;
                lableCopy.frame=newFrame;
                [lableCopy adjustsFontSizeToFitWidth];
                //lableCopy.backgroundColor=[UIColor blackColor];
                [searchField addSubview:lableCopy];
                lableCopy.text=lable.text;
                //lableCopy.textColor=[UIColor redColor];
            }

        }
        for (UIView *view in [[searchBar.subviews objectAtIndex:0] subviews]) {
            if([view isKindOfClass:[UITextField class]])
            {
                // NSLog(@"%@",view);
                NSLog(@"TextFieldPresent==>%@",view);
                if([view isFirstResponder])
                {
                    lable.hidden=NO;
                    lableCopy.hidden=YES;
                }
                else
                {
                    lable.hidden=YES;
                    lableCopy.hidden=NO;
                }
                break;
            }
        }

    }


    @end

This solution is just adding new UILable view and hide the existing placeholder to give the real feel of searchBar.Again redisplay the actual placeholder when search Bar became active.

This may be a temporary hack to fix that UI issue in IOS7.

When Inactive

When Active

OLD SOLUTION: [searchField setValue:[NSNumber numberWithBool:YES] forKeyPath:@"_placeholderLabel.adjustsFontSizeToFitWidth"];

will not work in ios7 because the size of lable used for disaplay the content is enough to show the text, the problem is the label width bug of ios7. it fails to re-size the label width.

there is little bit hack to fix this.

UITextField *searchField = [searchBar valueForKey:@"_searchField"];


    UILabel *lable=[searchBar valueForKey:@"_placeholderLabel"];
    lable.font=[UIFont fontWithName:lable.font.fontName size:10.0];

calculate the font-size based upon the search bar width of your own. I also tried to change the width of particular label but it never work.

-2
votes
    -(void)viewDidAppear:(BOOL)animated{

        self.searchBar.placeholder=@"woord hier invoeren";

    }


-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{

 self.searchBar.placeholder=@"woord hier invoeren.......";

}

enter image description here

-2
votes

I'm not here to give you a general solution but if you have a placeHolder to add, the dumbest and easy way to do it is by truncating the placeHolder yourself, so instead of

searchBar.placeholder = @"woord hier invoeren";

let it be

searchBar.placeholder = @"woord hier invo...";

i tried to mess with the private methods of apple but with no luck: the searchBar subviews are : -UISearchBarBackground. -UISearchBarTextField. leave the UISearchBarBackground aside the subviews of the UISearchBarTextField instance are : -_UISearchBarSearchFieldBackgroundView. -UIImageView. -UISearchBarTextFieldLabel.

what i went for is trying to mess with the rect (i emphasis on the word mess as these are private methods) of the UISearchBarTextFieldLabel coz i'm pretty sure that its frame ain't rendered right when the searchBar button is shown (bookmark), if you choose searchBar.showsBookmarkButton = NO; the placeHolder text will be truncated as expected. It's up to you, save yourself sometime and go with the dumb solution but that gets things done, or delve even further. keep up the good work.