UINeigationBar在UINavigationItem.titleView中没有完全填充宽度

时间:2012-11-02 06:59:10

标签: objective-c ios ios6

我在导航栏的titleView中有一个UISearchBar,我确实使用了[searchBar sizetofit]并使用CGRectZero的框架创建了UISearchBar。但是在导航的titleView中,UISearchBar左右两侧有一个小的空白区域。

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];

我希望UISearchBar能够在纵向和横向上完全填充屏幕宽度。

非常感谢任何帮助:)

我正在使用Xcode 4.5,iOS 6和iPhone 5。

我只想使用navigationItem.titleView来托管我的UISearchBar,我认为这样做更好。

全部谢谢

2 个答案:

答案 0 :(得分:0)

只需隐藏导航栏,然后将 theSearchBar 添加到self.view即可         self.navigationController.navigationBarHidden = NO;         [self.view addSubview:theSearchBar];

答案 1 :(得分:0)

在设置导航栏的背景图片后,只需在代码中添加这两行:

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

希望它适合你。