1
votes

I have a UISearchBar in a titleView of Navigation bar, i did use [searchBar sizetofit] and create UISearchBar with frame of CGRectZero. But there is a small empty space on the left and right size of the UISearchBar inside titleView of Navigation.

UISearchBar has empty on both side:

theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
theSearchBar.delegate = self;
self.navigationItem.titleView = self.theSearchBar;
[self.theSearchBar sizeToFit];

self.navigationItem.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

//Change color of UISearchBar
theSearchBar.tintColor = [UIColor colorWithRed:102.0f/255.0f green:73.0f/255.0f     
blue:126.0f/255.0f alpha:1.0f];

//Change color of NavigationBarItem
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:102.0f/255.0f 
green:73.0f/255.0f blue:126.0f/255.0f alpha:1.0f];

//Set background image for navigation bar
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:   
@"SearchBar.png"] forBarMetrics:UIBarMetricsDefault];

I would like the UISearchBar fully fill the width of screen in both portrait and landscape..

Any help is really appreciated :)

Im using Xcode 4.5, iOS 6, with iPhone 5.

I only want to use navigationItem.titleView to host my UISearchBar, i think its better this way.

Thanks all

2

2 Answers

0
votes

Simply you can hide the navigation bar, and add the theSearchBar to self.view self.navigationController.navigationBarHidden = NO; [self.view addSubview:theSearchBar];

0
votes

just add these two lines in your code after setting the background image for navigation bar :

[self.navigationItem setRightBarButtonItem:nil];
[self.navigationItem setLeftBarButtonItem:nil];

hope it works for you.