1

我在导航栏的标题视图中有一个 UISearchBar,我确实使用了 [searchBar sizetofit] 并创建了带有 CGRectZero 框架的 UISearchBar。但是Navigation的titleView里面的UISearchBar的左右大小有一个小的空白区域。

UISearchBar 两边都是空的:

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 完全填充纵向和横向屏幕的宽度..

非常感谢任何帮助:)

我在 iPhone 5 上使用 Xcode 4.5、iOS 6。

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

谢谢大家

4

2 回答 2

0

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

于 2012-11-02T07:10:05.053 回答
0

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

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

希望对你有效。

于 2012-11-02T07:32:53.923 回答