iOS UIModalPresentationFormSheet風格下的鍵盤隱藏

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   strong   

1. 在UIModalPresentationFormSheet(iPad device, without a UINavigationController)下的視圖中,如果使用

[inputView resignFirstResponder];

是不能把Keyboard收合的,需要使用以下的方式:

A:

    @try

    {

        Class UIKeyboardImpl = NSClassFromString(@"UIKeyboardImpl");

        id activeInstance = [UIKeyboardImpl performSelector:@selector(activeInstance)];

        [activeInstance performSelector:@selector(dismissKeyboard)];

    }

    @catch (NSException *exception)

    {

        NSLog(@"Exception : %@", exception);

    }

上面的方法由於用到了Apple的Private API, 將無法通過審核。

B. 之所以在UIModalPresentationFormSheet下的視圖無法用resignFirstResponder是因為在進入到此模式的後,系統將下列中方法傳回值置為了YES:

- (BOOL)disablesAutomaticKeyboardDismissal

{

    return YES;

}// disablesAutomaticKeyboardDismissal, 此方法是在UIViewController中。

將傳回值改為NO後,即可正常使用resignFirstResponsder方法隱藏鍵盤,但在UINavigationController中此方式依然失效,以下將補充說明此情況。

 

2. 如果你顯示的UIModalPresentationFormSheet視圖是在UINavigationController, 那麼在UINavigationController的rootViewController中重寫

disablesAutomaticKeyboardDismissal方法也將無法啟用resignFirstResponder方法效果,因為顯示的視圖是UINavigationController的,應該在UINavigationController中

來重寫disablesAutomaticKeyboardDismissal方法。這裡通過將擴充的方式來達到在UINavigationController中重寫此方法的目的:

#import <UIKit/UIKit.h>@interface UINavigationController (UINavigationController_KeyboardDismiss)- (BOOL)disablesAutomaticKeyboardDismissal;@end#import "UINavigationController+UINavigationController_KeyboardDismiss.h"@implementation UINavigationController (UINavigationController_KeyboardDismiss)- (BOOL)disablesAutomaticKeyboardDismissal{    return NO;}// disablesAutomaticKeyboardDismissal@end

經過UINavigationController Category對disablesAutomaticKeyboardDismissal方法的重寫後,即可解決resignFirstResponder方法失效的問題。

 

參考:http://stackoverflow.com/questions/3372333/ipad-keyboard-will-not-dismiss-if-modal-view-controller-presentation-style-is-ui

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.