Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar placeholder aligning and cropping in iOS 7

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.

like image 475
Mike Keskinov Avatar asked Oct 08 '13 20:10

Mike Keskinov


1 Answers

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.

like image 183
CoolMonster Avatar answered Oct 16 '22 11:10

CoolMonster