我知道這是一箇舊的,但如果其他人正在尋找一個簡單的答案,這是我的解決方案,以獲得一個「mapView」resignFirstResponder。這將以類似於iphone上的Google地圖應用的方式工作,其中在開始編輯搜索文本字段時出現半透明框。
首先,你需要讓你的視圖控制器UISearchBarDelegate
@interface ViewControllerName <UISearchBarDelegate>
// your code here
@end
然後執行以下的委託方法:
@implementation ViewControllerName
// your code here
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
darkBg = [[UIControl alloc] initWithFrame:CGRectMake(0, 244, 320, 300)];
[darkBg setBackgroundColor:[UIColor blackColor]];
[darkBg addTarget:nil action:@selector(hideKeyboard) forControlEvents:UIControlEventTouchDown];
[darkBg setAlpha:0.8];
[UIView beginAnimations:@"slideup" context:nil];
[darkBg setCenter:CGPointMake(160, 194)];
[self.view addSubview:darkBg];
[UIView commitAnimations];
}
- (void)hideKeyboard {
[UIView beginAnimations:@"fadeou" context:nil];
[darkBg setAlpha:0.0];
[UIView commitAnimations];
[addressField resignFirstResponder];
[darkBg performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.0];
[darkBg release];
}
來源
2010-11-04 21:29:14
Rog
感謝您使用UISearchBar的提示。我會嘗試。我的問題是,我不知道如何檢測用戶何時單擊mapview,所以我無處可以寫這行代碼:) – Imran 2010-01-30 17:44:45
在添加代碼獲取觸摸。 – FelixLam 2010-01-30 18:22:46
我不認爲你可以做到這一點!請參閱:http://stackoverflow.com/questions/1049889/how-to-intercept-touches-events-on-a-mkmapview-or-uiwebview-objects。如果我誤解,請告訴我。我正在考慮在鍵盤啓動時覆蓋地圖上的透明視圖,並從中獲取相關信息(我的鏈接中接受的解決方案看起來過於複雜)。 – Imran 2010-01-30 18:50:33