背景:我有一個UIViewController,它具有以編程方式添加的UITableView。在UITableView的頂部,我有一個tablHeaderView,其中放置了一個UIView。這個UIView有一個UISearchBar和一個segmentedControl。這個想法是:用戶可以按照'日期/時間'或'位置'等基本類別對UITableView進行排序,也可以通過程序中的項目進行搜索。將UISearchBar添加到UITableView
問題:當我點擊搜索欄時,它調整大小(我不想),然後當我取消搜索時,它再次調整大小,直到UIViewController退出並重新加載。
代碼:
-(void)loadTableView
{
usableSpace = [[UIScreen mainScreen] applicationFrame];
usableWidth = usableSpace.size.width;
usableHeight = usableSpace.size.height;
_tableView = [[UITableView alloc] init];
[_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
[_tableView setDataSource:self];
[_tableView setDelegate:self];
[self.view addSubview:_tableView];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
_searchBar.showsCancelButton = YES;
NSLog(@"searchBar height = %fl", _searchBar.frame.size.height);
segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Date/Time", @"Location", @"Speaker", nil]];
segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
[segmentedControl addTarget:self action:@selector(sortList) forControlEvents:UIControlEventValueChanged];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
[headerView addSubview:_searchBar];
[headerView addSubview:segmentedControl];
_tableView.tableHeaderView = headerView;
[self.view addSubview:_tableView];
self.tableView = _tableView;
}
我曾嘗試:我已經嘗試設置搜索欄的大小,而不是設置其大小。沒有它在tableHeaderView中的UIView中,而只是在它自己的。
爲什麼它調整大小,我該如何讓它停下來?
編輯:我剛纔試過如下:在故事板(其中的UIViewController最初創建)我在問題中選擇的UIVC並在屬性檢查器中我取消選擇「在最熱門酒吧」和「下底杆」和這似乎已經解決了動畫問題的第一部分。現在,當我點擊搜索欄時,搜索變得活躍,但searchBar不會調整大小。但是,當我取消搜索欄時,searchBar仍會按照截圖中的最後一張圖片進行動畫和調整大小。什麼可能導致調整大小?
我有同樣的問題,我嘗試使用此解決方案。它工作正常。 –