標籤:
最近遇到二個鍵盤會自動彈出的問題:
1、UIWebView載入網頁後,點擊網頁內的連結在UIWebView內進行跳轉時,鍵盤自動彈起;
2、調用選擇照片時,iPod上選擇照片後也會自動彈出鍵盤,比如從圖庫進到具體某個檔案夾內,或者再返回圖庫,直接點中照片然後編輯的時候,都會自動彈出;
問題一是這樣處理的
- (void)webViewDidStartLoad:(UIWebView*)webView
{
[activityIndicatorView_ startAnimating];
// update by zhangyi
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
- (void)webViewDidFinishLoad:(UIWebView*)webView
{
[activityIndicatorView_ stopAnimating];
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
- (void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error
{
[activityIndicatorView_ stopAnimating];
UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alterview show];
[alterview release];
}
網頁開始載入或載入結束時關閉鍵盤並發送關閉鍵盤的事件,如果webViewDidFinishLoad()不被調用是因為uiwebview對象需要設定delegate為self。UIWebView的透明設定,只需要增加二個屬性
webView_.opaque = NO;
webView_.backgroundColor = [UIColor clearColor];
第二個問題的處理方案是,在導航切換的時候隱藏鍵盤並發送關閉鍵盤的事件
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
viewController.contentSizeForViewInPopover = navigationController.topViewController.view.frame.size;
[self HiddenStatusBarForIOS7];
// update by zhangyi
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
// update by zhangyi
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
因為接觸ios也不算特別多,暫時就使用了上述的方法來處理了。
參考:
How to make a transparent UIWebView
Add an activity indicator on a uiwebview
IOS隱藏鍵盤