[英]slide form up when keyboard appears
您好,我正在使用表單進行數據輸入,一些文本字段位於表單的底部。 當我單擊文本字段進行書寫時,鍵盤會出現並隱藏它后面的字段。 如果我讓文本字段第一響應者隱藏鍵盤,但這樣做我無法做到這一點。 我想知道當鍵盤出現時,整個表單應該如何向上移動,就像我的最后一個字段出現在鍵盤的操作上一樣提前謝謝
在滾動視圖中添加所有視圖並設置滾動視圖和文本字段的委托,然后使用這些委托和方法 -
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[self scrollViewToCenterOfScreen:textField];
return YES;
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
if ([textField isEqual:txtField1])
{
[txtField2 becomeFirstResponder];
}
else if ([textField isEqual:txtField2])
{
[txtField3 becomeFirstResponder];
}
else
{
[textField resignFirstResponder];
[scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
return YES;
}
- (void)scrollViewToCenterOfScreen:(UIView *)theView {
CGFloat viewCenterY = theView.center.y;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGFloat availableHeight = applicationFrame.size.height - 200; // Remove area covered by keyboard
CGFloat y = viewCenterY - availableHeight / 2.0;
if (y < 0) {
y = 0;
}
[scrollView setContentOffset:CGPointMake(0, y) animated:YES];
}
您可以使用多種策略來執行此操作——其中大多數涉及將您的視圖包裝在 UIScrollView 中,並在獲得焦點時滾動到適當的控件。 您可以在此處找到有關如何執行此操作的 Apple 文檔: http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.