iOS - UIModalPresentationFormSheet模式下resignFirstResponder无法隐藏键盘

本文总结了在iOS中关闭虚拟键盘的多种方法,包括重写disablesAutomaticKeyboardDismissal方法、通过添加工具栏按钮触发键盘关闭、利用UINavigationController的Category实现等。
iOS 弹出UIModalPresentationFormSheet模式
 
    UIViewController*fbsheet = [[FeedbackSheet alloc] initWithNibName:@"FeedbackSheet"bundle:nil];  
    fbsheet.modalPresentationStyle = UIModalPresentationFormSheet; 
    [self presentModalViewController:fbsheet animated:YES]; 
     
但是当在UItextview软盘弹出之后,点击其他区域使用下面代码软盘无法隐藏
方法一:
重写disablesAutomaticKeyboardDismissal即可,该api在iOS (4.3 andlater)
-(BOOL)disablesAutomaticKeyboardDismissal 
{  
    return NO; 
}  

方法二:

  - (void)viewDidLoad 
{  
    [super viewDidLoad]; 
     
    UIToolbar * topView =[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; 
    [topViewsetBarStyle:UIBarStyleBlack];  
  
    UIBarButtonItem *btnSpace = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:self action:nil];  
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"收起键盘"style:UIBarButtonItemStyleDone target:selfaction:@selector(dismissKeyBoard)];  
    [doneButtonsetWidth:80];  
     
    NSArray * buttonsArray =[NSArray arrayWithObjects:btnSpace,doneButton,nil]; 
    [doneButton release]; 
    [btnSpace release]; 
     
    [topViewsetItems:buttonsArray];  
    [feedbackContentsetInputAccessoryView:topView];  
}  
  
-(IBAction)dismissKeyBoard  
{  
    [feedbackContentresignFirstResponder];  
}

方法三:

 UINavigationController *nv =[[UINavigationController alloc]initWithRootViewController:searchVC];
   nv.modalPresentationStyle = UIModalPresentationFormSheet;
    [selfpresentModalViewController:nv animated:YES];

UINavigationController必须用Category的方法实现如下方法,才可以让键盘消失
@interface UINavigationController (DismissKeyboard)
- (BOOL)disablesAutomaticKeyboardDismissal;
@end

@implementation UINavigationController (DismissKeyboard)

- (BOOL)disablesAutomaticKeyboardDismissal
{
    return NO;
}

方法四:此方法是能解决关闭键盘,可是苹果审核会拒绝使用该api

[textF resignFirstResponder];//没起到键盘收起的效果
    @try {
       Class UIKeyboardImpl =NSClassFromString(@"UIKeyboardImpl");
       id activeInstance = [UIKeyboardImplperformSelector:@selector(activeInstance)];
       [activeInstanceperformSelector:@selector(dismissKeyboard)];
    }
    @catch (NSException*exception) {
       NSLog(@"%@", exception);
    }

方法五:

    //关闭键盘
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
 
  如果一个view上好多输入框,为了关闭弹出的软键盘要遍历然后调用resignFirstResponder的吧?发现一个关键这个view上可能    打开的键盘很简单:[self.view endEditing:YES];
   直接ok了。

方法六:

   //关闭键盘
    [[self findFirstResponderBeneathView:self] resignFirstResponder];
 
[[self findFirstResponderBeneathView:self] becomeFirstResponder];//打开键盘
-(UIView*)findFirstResponderBeneathView:(UIView*)view 
{
    // Search recursivelyfor first responder
    for ( UIView *childViewin view.subviews ) {
       if ( [childViewrespondsToSelector:@selector(isFirstResponder)]&& [childView isFirstResponder]) 
           returnchildView;
       UIView *result = [selffindFirstResponderBeneathView:childView];
       if ( result ) 
           returnresult;
    }
    return nil;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值