0

Purpose: I want to set right view as label in searchbar's textfield

Following Code working fine in ios 6 for doing the same thing:

UISearchBar  *search = [[UISearchBar alloc] init];
[search setTintColor:[UIColor grayColor]];
search.frame = CGRectMake(0, 150, 320,50);
search.delegate = self;

UITextField *searchField=[search.subviews objectAtIndex:1];//Changed this line in ios 7
searchField.backgroundColor=[UIColor redColor];

UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text=@"Hello";
label.textColor=[UIColor greenColor];

searchField.rightView = label;
searchField.rightViewMode = UITextFieldViewModeAlways;

[self addSubView:search];

When i have Run the same code in ios 7(with xcode 7.0.2 fully updated), It was giving me error, after googling i got that hiearchy of searchbar to textfield has been changed, so i have modified my code as:

replaced commented line in above code with:

UITextField *searchField=[((UIView *)[search.subviews objectAtIndex:0]).subviews lastObject];

after doing this, it is not giving me any error(also checked the logs by printing the description of textfield and everything is working good), when i run it, it gives textfield with red background but the rightView's label is not shown to me. Now, can anyone help me how to show this rightview.

Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81

3 Answers3

8

If you want to do this programatically then you have to declare UISearchBar object in your .h file.

UISearchBar  *searchBar;

And in your .m file you can code like:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
    searchBar = [[UISearchBar alloc] init];
    [searchBar setTintColor:[UIColor grayColor]];
    searchBar.frame = CGRectMake(0, 150, 320,50);
    searchBar.delegate = self;

    [self.view addSubview:searchBar];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    UITextField *searchField;
    if ([[[UIDevice currentDevice] systemVersion] floatValue]<7.0)
        searchField=[searchBar.subviews objectAtIndex:1];
    else
        searchField=[((UIView *)[searchBar.subviews objectAtIndex:0]).subviews lastObject];

    searchField.backgroundColor=[UIColor redColor];

    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    label.text=@"Hello";
    label.textColor=[UIColor greenColor];

    searchField.rightView = label;
    searchField.rightViewMode = UITextFieldViewModeAlways;
}

I think this will work for both iOS7 and prior versions of iOS7.

Yuvrajsinh
  • 4,536
  • 1
  • 18
  • 32
  • The problem is actually with the hierarchy of the views in iOS7 is for some views like UISearchBar, UIAlertView etc. so the view customisation code working in prior versions of iOS is not working in iOS7. – Yuvrajsinh Oct 22 '13 at 09:49
  • if i want to do this all stuff only at one place, in any method, can't i do that?, is it necessary that i must have to do this customization in viewDidAppear or in any other method – Mehul Thakkar Oct 22 '13 at 09:53
  • It's better to do customization in viewDidAppear because if you do initialization and customization in place then customization code will be executed prior to some inner setup of UISearBar and so you will not get the exact result you want. – Yuvrajsinh Oct 22 '13 at 10:05
4

First of all, your approach is not good and not maintainable across iOS versions. I think that initialization of UISearchBarTextField's right view is taking place after you set your custom view to it's rightView property. You will see correct result if you set searchField.rightView in viewDidAppear method:

- (void)viewDidAppear:(BOOL)animated {
     [super viewDidAppear:animated];
     UITextField *searchField=[((UIView *)[_searchBar.subviews objectAtIndex:0]).subviews lastObject];

     UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
     label.text=@"Hello";
     label.textColor=[UIColor greenColor];

     searchField.rightView = label;
     searchField.rightViewMode = UITextFieldViewModeAlways;
}

I've tested it on iOS 7 simulator and it works.

opedge
  • 1,532
  • 1
  • 10
  • 22
  • Great, this is working for me if i make searchbar in xib and do this all thing, but if i try to make searchbar using code and then trying to put rightView then it is not working, can you suggest me how to do it by programming – Mehul Thakkar Oct 22 '13 at 09:03
0

You have to get the text field first.
No need to index subviews to get text field or bar button.

You can directly get the text field and bar button

In swift

let textField = searchBar.valueForKey("searchField") as? UITextField
let cancelBarButton = searchBar.valueForKey("cancelBarButtonItem") as? UIBarButtonItem

In Objective-C

UITextField *textField = [searchBar valueForKey:@"searchField"]:
UIBarButtonItem *cancelBarButton = [searchBar valueForKey:@"cancelBarButtonItem"]:

Then you can set the left view and right view with

textField.leftView = yourView
textField.rightView = yourView
Edward Anthony
  • 3,354
  • 3
  • 25
  • 40