![](/img/trans.png)
[英]How to refresh my UITableView with UISearchBar after click of cancel button of UISearchBar?
[英]TableViewController UISearchBar after rotation cancel button not clickable
我有一個TableVieController,它的searchBar定位在self.tableView.tableHeaderView上 。 屏幕旋轉時,我只是將框架設置為新的CGRect。 該視圖看起來不錯,但不幸的是,搜索欄的一半不可單擊,取消按鈕也不可單擊。
那我該如何做呢? 我只能通過創建搜索欄的新實例並分配它來做到這一點,但這不是正確的方法,對嗎?
const CGRect CGRECT_TABLE_VIEW_PORTRAIT = { { 0.0f, 0.0f }, { 320.0f, 365.0f } };
const CGRect CGRECT_TABLE_VIEW_LANDSCAPE = { { 0.0f, 0.0f }, { 480.0f, 150.0f } };
- (void)viewDidLoad
{
...
// create search bar
searchBar = [[UISearchBar alloc] initWithFrame:self.tableView.bounds];
[searchBar setTintColor:RGB(168, 212, 255)] ;
searchBar.showsCancelButton = YES;
[searchBar sizeToFit]; // Get the default height for a search bar.
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
self.tableView.frame = CGRECT_TABLE_VIEW_PORTRAIT;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
tableView.frame = CGRECT_TABLE_VIEW_LANDSCAPE;
} else {
tableView.frame = CGRECT_TABLE_VIEW_LANDSCAPE;
}
}
我不知道您為什么要嘗試輪換處理,這種情況應該自動發生。 這段代碼的作用是提供一個可以根據旋轉程度進行調整的搜索欄:
- (void)viewDidLoad {
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:self.tableView.bounds];
[searchBar setTintColor:[UIColor colorWithRed:168/255 green:212/255 blue:1 alpha:1]];
searchBar.showsCancelButton = YES;
[searchBar sizeToFit]; // Get the default height for a search bar.
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.