2017-09-14 65 views
7

與已發佈的問題相同(非答案工作..),我的表是固定寬度,並且在iOS 10.x中,搜索欄(它在tableviewheader中),在打字時保持相同的尺寸。但是,在iOS 11中,它跳轉到屏幕的頂部並在整個寬度上伸展。所有選項都嘗試過iOS 11 Searchcontroller跳轉屏幕上方

self.definesPresentationContext = YES; 
tableHeaderView.clipsToBounds = YES; 
etc 

但似乎沒有任何改變。它仍然跳轉到頂部全寬..

任何其他的選擇嗎?

的Xcode 9 GM與iOS 11(9 Xcode的GM與iOS 10.x和這一切工作正常)

回答

1

首先,你需要添加UISearchControllerDelegate

然後設置您的代理

self.searchController.delegate=self; 

放置一個新的視圖,將舉行您的搜索控制器

-(void) viewDidLoad{ 
    UIView *searchContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.searchTable.frame.size.width, 30)]; 
    searchContainerView.layer.masksToBounds=YES; 
    searchContainerView.clipsToBounds=YES; 
    self.tableView.tableHeaderView=searchContainerView; 
    self.searchController.searchBar.layer.masksToBounds=YES; 
    self.searchController.searchBar.clipsToBounds=YES; 
    [searchContainerView addSubview:self.searchController.searchBar]; 
    [self.searchController.searchBar setFrame:CGRectMake(0, 0, 300, 30)];//Make sure that your header view and searchcontroller size is same 
} 

然後添加th ESE委託方法

- (void)didPresentSearchController:(UISearchController *)searchController{ 
    [searchController.searchBar setFrame:CGRectMake(0, 0, 300, 30)]; 
} 
- (void)didDismissSearchController:(UISearchController *)searchController{ 
    [searchController.searchBar setFrame:CGRectMake(0, 0, 300, 30)]; 
} 

爲我工作,我希望你能使其工作

+2

我能夠得到這個代碼來調整搜索欄的寬度,但我的搜索欄還是跳出的我的桌子視圖的頂部,並轉到屏幕的頂部邊緣。如果我嘗試在didPresentSearchController中移動它,那麼儘管給它一個很大的高度或更大的Y位置,它會開始被奇怪地切斷。 –

+0

你可以發佈一些你的代碼,我找不出什麼是缺少的。 – BaranBerk