2012-11-02 29 views
1

我在導航欄的titleView中有一個UISearchBar,我沒有使用[searchBar sizetofit]並創建帶有CGRectZero框架的UISearchBar。但是在導航的titleView中,UISearchBar的左右尺寸上有一個很小的空白空間。UINearchBar在UINavigationItem.titleView中沒有完全填充寬度

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,我認爲它更好的方式。

感謝所有

回答

0

只要你可以隱藏導航欄,並添加theSearchBar到self.view self.navigationController.navigationBarHidden = NO; [self.view addSubview:theSearchBar];

+0

我試過這個,但是searchBar並沒有在橫向視圖中擴展它的大小。當用戶旋轉時,如何擴展和縮回?你如何從導航欄中刪除它的子視圖? –

+0

我把它與autarchizingmask爲herearchbar :)並將其隱藏在視圖中消失。謝謝 –

0

只是在你的代碼中添加以下兩行的導航欄設置背景圖片後:

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

希望它爲你工作。

+0

不幸的是它並沒有解決問題:( –