在我的应用程序中,我有一些通过键盘填充的文本字段。其中有6个,最后两个位于屏幕的最底部,只要键盘出现就会隐藏。 有什么建议??
答案 0 :(得分:0)
使用UIScrollview以便用户向下滚动。您可以使用UITextField的inputAccessoryView来显示下一个和上一个按钮,点击让输入跳转到下一个文本字段并滚动到它。
在SO处查看此question and answers,同样的问题,不同的解决方案。
答案 1 :(得分:0)
将textFields委托为self,并将以下代码添加到textFieldDidBeginEditing和textFieldDidEndEditing。
定义以下常量:(已为iPad设置常量。为iphone调整它们)
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 302;
在班级的.h文件中添加两个变量。 CGFloat animatedDistance;和UITextField * currentField;
- (void)textFieldDidBeginEditing:(UITextField *)textField {
currentField = textField;
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
CGRect textFieldRect =[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =[self.view.window convertRect:self.view.bounds fromView:self.view];
if (orientation == UIInterfaceOrientationLandscapeLeft )
{
textFieldRect.origin.y = textFieldRect.origin.x;
textFieldRect.size.height = textFieldRect.size.width;
viewRect.size.height = viewRect.size.width;
}
else if (orientation == UIInterfaceOrientationLandscapeRight )
{
textFieldRect.origin.y =viewRect.size.width - textFieldRect.origin.x;
textFieldRect.size.height = textFieldRect.size.width;
viewRect.size.height = viewRect.size.width;
}
else if (orientation == UIInterfaceOrientationPortraitUpsideDown)
{
textFieldRect.origin.y = viewRect.size.height - textFieldRect.origin.y;
}
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
animatedDistance = 0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
currentField = nil;
}