Search code examples
iosobjective-cuisearchbaruisearchdisplaycontroller

Two UISearchBars showing up in a view controller when only one is instantiated


I have two search bars shhowing up in a view controller. It's strange because I have the same exact code in another vc and it works fine. (the search bar in the background shouldn't be there)

here's a screenshot:

enter image description here

I added the delegates: <UISearchBarDelegate, UISearchDisplayDelegate>

then in the .h:

@property (nonatomic,retain) IBOutlet UISearchBar *searchBar;

in the .m:

@synthesize searchBar;

in the viewDidLoad:

 self.searchBar.frame = CGRectMake(204, 11, 107,44);
    self.searchBar.delegate = self;

    //customize the searchbar
    UITextField *searchField = [self.searchBar valueForKey:@"_searchField"];
    [searchField setBackground:[UIImage imageNamed:@"search_panel.png"]];
        
    [self.searchBar setTintColor:[UIColor redColor]];
    UIImage *searchimg = [UIImage imageNamed:@"searchfield_bg.png"];
    
    for (UIView *subview in self.searchBar.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
            bg.backgroundColor = [UIColor colorWithPatternImage:searchimg];
            [self.searchBar insertSubview:bg aboveSubview:subview];
            [subview removeFromSuperview];
            break;
        }
    }
    
    [self.view addSubview:self.searchBar];

and that's it. I have nothing to do with the searchBar in the Storyboard view controller xib it's added programmatically in the viewDidLoad method

thanks for any help


Solution

  • If you're using an outlet, your UISearchBar very likely also exists in the storyboard or the xib. Since you're also creating it in the viewDidLoad, you're making a second copy of it. Take another look at your storyboard.