1

UITextField表示された後にキーボードのrectを取得する必要があるカスタムクラスがあります。それで、クラスで私UIKeyboardWillShowNotificationはキーボードのrectを聞いて取得しUIKeyboardFrameEndUserInfoKeyます。これは、キーボードが現在表示されていない場合にうまく機能します。ただし、テキストフィールド間をタップしたときなど、キーボードがすでに表示されている場合はUIKeyboardWillShowNotification、最初にタップされたテキストフィールドに対してのみ起動します。他のすべてのテキストフィールドについては、キーボードの長方形が何であるかを知る方法がありません。

キーボードがすでに表示された後、どのようにキーボードの長方形を取得するかについての提案はありますか?

4

1 に答える 1

1

AppDelegateあなたのkeyboardFrame財産を与えます。プロパティを適切にAppDelegate監視UIKeyboardWillShowNotificationおよびUIKeyboardWillHideNotification更新します。キーボードが非表示のときにプロパティを設定するCGRectNullか、別のプロパティを追加しkeyboardIsShowingます。CGRectNull(関数の使用をテストできCGRectIsNullます。)

次に、この呪文を使用して、任意のオブジェクトがいつでもキーボード フレームをチェックできます。

[[UIApplication sharedApplication].delegate keyboardFrame]

これをアプリのデリゲートに入れたくない場合は、別のシングルトン クラスを作成できます。

@interface KeyboardProxy

+ (KeyboardProxy *)sharedProxy;

@property (nonatomic, readonly) CGRect frame;
@property (nonatomic, readonly) BOOL visible;

@end

@implementation KeyboardProxy

#pragma mark - Public API

+ (KeyboardProxy *)sharedProxy {
    static dispatch_once_t once;
    static KeyboardProxy *theProxy;
    dispatch_once(&once, ^{
        theProxy = [[self alloc] init];
    });
    return theProxy;
}

@synthesize frame = _frame;

- (BOOL)visible {
    return CGRectIsNull(self.frame);
}

#pragma mark - Implementation details

- (id)init {
    if (self = [super init]) {
        _frame = CGRectNull;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)keyboardWillShow:(NSNotification *)note {
    _frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
}

- (void)keyboardWillHide:(NSNotification *)note {
    _frame = CGRectNull;
}

@end

ただし、別のシングルトンを使用する場合は[KeyboardProxy sharedProxy]、アプリのデリゲートから呼び出しapplication:didFinishLaunchingWithOptions:て、シングルトンが通知を見逃さないようにする必要があります。

于 2013-03-22T05:36:23.473 に答える