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:
て、シングルトンが通知を見逃さないようにする必要があります。