前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IOS隐藏键盘

IOS隐藏键盘

作者头像
meteoric
发布2018-11-19 18:07:38
1.3K0
发布2018-11-19 18:07:38
举报
文章被收录于专栏:游戏杂谈游戏杂谈游戏杂谈

最近遇到二个键盘会自动弹出的问题:

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-05-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档