[英]UITextField moving when keyboard will block it
我試圖這樣做,以便當您單擊電子郵件字段並彈出鍵盤時,它將向上移動視圖。 但是現在使用此代碼,無論我點擊什么文本字段,它都會移動視圖。 我也不能讓鍵盤解雇。 不確定如何將此代碼設置為僅滾動到活動字段?
碼:
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, self.emailField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, self.emailField.frame.origin.y-kbSize.height);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
我有一個看起來像這樣的視圖(它在滾動視圖上)
為此,您可以忽略鍵盤顯示/隱藏通知,只需使用UITextFieldDelegate
協議:
– (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if([textField isEqual:self.emailTextField]){
// scroll up
}
return true;
}
– (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if([textField isEqual:self.emailTextField]){
// scroll back to start
}
return true;
}
– (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if([textField isEqual:YourTextfieldName])
{
}
return true; // return true is needed.
}
– (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if([textField isEqual:YourTextfieldName])
{
}
return true; //return true is needed.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.