[英]Test whether keyboard hide a text field when it's shown
當顯示鍵盤時,我嘗試將表格向上移動,我的方法是測試鍵盤的框架和文本字段的框架是否相交。
- (void)keyboardDidShow:(NSNotification *)notification
{
// Get the size of the keyboard.
CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
//Test whether the current frame of the text field is hidden by the keyboard
if (!CGRectIsNull(CGRectIntersection(keyboardFrame,self.activeField.frame))) {
NSLog(@"Key board frame intersects with the text field frame");
}
}
在上面的代碼中, CGRectIsNull
始終返回null。
一條調試語句將以下有關鍵盤和活動文本字段的信息返回給我:
鍵盤大小=(寬度= 352,高度= 1024)鍵盤原點=(x = -352,y = 0)
鍵盤框架=(-352,0,352,1024)文本字段框架=(200、15、300、30)
每個文本字段都具有相同的框架值,這意味着有問題。 因此,如何測試鍵盤是否隱藏了文本字段,以便我上下移動表單。 謝謝
我會將整個東西放到全屏UIScrollView中,然后在需要時調整其大小...
// This in your init somewhere
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
-(void) keyboardWillChange:(NSNotification*)notify {
CGRect endFrame;
float duration = [[[notify userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[[[notify userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&endFrame];
endFrame = [self.view convertRect:endFrame fromView:nil];
float y = (endFrame.origin.y > self.view.bounds.size.height ? self.view.bounds.size.height : endFrame.origin.y);
[UIView animateWithDuration:duration animations:^{
scrollView.frame = CGRectMake(0, 0, self.view.bounds.size.width, y);
}];
}
嘗試這個:
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {}];
並使用[note userInfo];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.