![](/img/trans.png)
[英]Creating UIView in UITableViewController that doesn't moves with the table view
[英]UITableViewController - Drop down table view to show a new UIView
我正在嘗試設計一個UITableViewController,當用戶點擊導航欄上的“搜索”按鈕時,表格視圖會下降,UIView會從導航欄下拉。 然后,當用戶點擊UIView之外的任何地方時,UIView應該縮回並且表視圖應該返回到其原始位置。
目前,我有以下方法,即“搜索”按鈕的選擇器。 注意 - self.searchBar是一個自定義的UIView子類。
是否有更清潔/更好的方法來實現這一目標? 在用戶點擊搜索菜單后,我也不確定如何擺脫視圖......我猜我應該打電話,[self.searchBar removeFromSuperview]; 但不確定將該行放在哪個委托方法中。
謝謝!
- (void)_showSearchMenu
{
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * .25);
frame.origin.y = CGRectGetMaxY(self.navigationController.navigationBar.frame) - frame.size.height;
self.searchBar.frame = frame;
[self.navigationController.navigationBar.superview insertSubview:self.searchBar belowSubview:self.navigationController.navigationBar];
[UIView animateWithDuration:0.5 animations:^{
CGRect frame = self.searchBar.frame;
frame.origin.y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
self.searchBar.frame = frame;
self.tableView.contentOffset = CGPointMake(0, -250);
}];
}
為了更清楚,我試圖在這里嘗試類似於HotelTonight應用程序中看到的效果(第二個屏幕顯示當您點擊右上方的按鈕時會發生什么)
這是我認為最好的方法,使用這些代表:
(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
怎么樣:
BOOL isOpen
,默認值為NO
單擊“搜索”按鈕時,請執行以下操作:
(void) searchButtonTouch:(id)sender { isOpen = (isOpen) ? YES : NO; isOpen = !isOpen; [self.urTableView reloadData]; }
現在在你的代表中:
(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return (isOpen) ? 170.0f : 0.0f; } (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { CGFloat height = [self tableView:tableView heightForHeaderInSection:section]; UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, height)]; vw.backgroundColor = [UIColor yellowColor]; // add other controls here in your UIView // or // just add a UIView at top of your UITableView // then add other controls to it (if ur using storyboard) return vw; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.