IOS隱藏鍵盤

來源:互聯網
上載者:User

標籤:

最近遇到二個鍵盤會自動彈出的問題:

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隱藏鍵盤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.