0

我在iOS 8中使用UISearchController,當我使它的UISearchBar我的表頭視圖一切都很好。但是現在我需要它在我的UINavigationBar的左邊欄按鈕中(因爲放大導航欄時垂直居中,所以不能是標題視圖)。當我將UISearchBar包裝在UIBarButtonItem中時,它大於屏幕的寬度。如何適合UIBarButtonItem中的UISearchBar?

off screen search bar

我認爲這是關係到我從大小類故事板初始化視圖控制器啓用,這意味着我的框架沒有設置直到viewDidLayoutSubviews。不過,我在工具欄中遇到了與我的分段控件相同的問題,我只需在viewDidLayoutSubviews的工具欄上調用sizeToFit並修復了工具欄。當我對搜索欄執行相同操作時,它仍然會部分脫離屏幕。蘋果在他們的一些iPad應用程序中這樣做,但是怎麼做?

注意:我可以在屏幕上將其全部隱藏起來,但我必須將其包裝在另一個視圖中,但顏色已關閉,而且看起來錯誤,我認爲動畫已關閉。

回答

1

你需要用UIBarButtonSystemItemFixedSpace創建一個帶有-10寬度的UIBarButtonItem,它將移除10個px填充。

UIBarButtonItem *Spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; 
[Spacer setWidth:-10]; 
UISearchBar *searchBar= [[UISearchBar alloc] initWithFrame:CGRectMake(0, 5, self.view.frame.size.width-10, 39)]; 
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar]; 
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:Spacer,searchBarItem,nil]; 
相關問題