iOS 在键盘之前调出 UIView?
iOS Bring UIView up before keyboard?
我的 UIView 中有两个 UIView。这是我的观点:
在聚焦文本字段时,显示键盘,而键盘隐藏了我的文本字段。我想在键盘之前将整个 TextField View 向上移动。我在 Whosebug 上找到了代码示例:
iPhone Keyboard Covers UITextField
这是代码:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField: textField up: YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField: textField up: NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 80; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
我正在尝试实现它,但遇到了问题。有人可以解释如何正确地做到这一点吗?
而不是手动执行此操作。使用 TPKeyboardAvoidingScrollView。
它易于使用。
首先获取 UIScrollView 并将所有视图放入其中。
为了与 UITableViewController classes 一起使用,将 TPKeyboardAvoidingTableView.m 和 TPKeyboardAvoidingTableView.h 放入您的项目中,并在 xib 中将您的 UITableView 设为 TPKeyboardAvoidingTableView。如果您的控制器没有使用 xib,我知道没有简单的方法可以使其 UITableView 成为自定义 class:阻力最小的途径是为其创建一个 xib。
对于非 UITableViewControllers,将 TPKeyboardAvoidingScrollView.m 和 TPKeyboardAvoidingScrollView.h 源文件放入项目中,将 UIScrollView 弹出到视图控制器的 xib 中,将滚动视图的 class 设置为 TPKeyboardAvoidingScrollView,并将所有控件放在该滚动视图中。您也可以在不使用 xib 的情况下以编程方式创建它 - 只需使用 TPKeyboardAvoidingScrollView 作为您的顶级视图。
您可以使用如下方式手动编写它:
- (void)makeRoomForKeyboard
{
UIView *rootView = [[[UIApplication sharedApplication]delegate] window].rootViewController.view.subviews[0];
[UIView animateWithDuration:0.2f animations:^{
rootView.center = CGPointMake(rootView.center.x, rootView.center.y - kKeyboardOffset);
} completion:nil];
}
并根据需要使用 kKeyboardOffset。
从 textFieldDidBeginEditing 中调用它。
完成编辑后,只需在 kKeyboardOffset 前面加一个 + 号即可调用相同的函数。
我的 UIView 中有两个 UIView。这是我的观点:
在聚焦文本字段时,显示键盘,而键盘隐藏了我的文本字段。我想在键盘之前将整个 TextField View 向上移动。我在 Whosebug 上找到了代码示例: iPhone Keyboard Covers UITextField
这是代码:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField: textField up: YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField: textField up: NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 80; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
我正在尝试实现它,但遇到了问题。有人可以解释如何正确地做到这一点吗?
而不是手动执行此操作。使用 TPKeyboardAvoidingScrollView。 它易于使用。
首先获取 UIScrollView 并将所有视图放入其中。
为了与 UITableViewController classes 一起使用,将 TPKeyboardAvoidingTableView.m 和 TPKeyboardAvoidingTableView.h 放入您的项目中,并在 xib 中将您的 UITableView 设为 TPKeyboardAvoidingTableView。如果您的控制器没有使用 xib,我知道没有简单的方法可以使其 UITableView 成为自定义 class:阻力最小的途径是为其创建一个 xib。
对于非 UITableViewControllers,将 TPKeyboardAvoidingScrollView.m 和 TPKeyboardAvoidingScrollView.h 源文件放入项目中,将 UIScrollView 弹出到视图控制器的 xib 中,将滚动视图的 class 设置为 TPKeyboardAvoidingScrollView,并将所有控件放在该滚动视图中。您也可以在不使用 xib 的情况下以编程方式创建它 - 只需使用 TPKeyboardAvoidingScrollView 作为您的顶级视图。
您可以使用如下方式手动编写它:
- (void)makeRoomForKeyboard
{
UIView *rootView = [[[UIApplication sharedApplication]delegate] window].rootViewController.view.subviews[0];
[UIView animateWithDuration:0.2f animations:^{
rootView.center = CGPointMake(rootView.center.x, rootView.center.y - kKeyboardOffset);
} completion:nil];
}
并根据需要使用 kKeyboardOffset。 从 textFieldDidBeginEditing 中调用它。 完成编辑后,只需在 kKeyboardOffset 前面加一个 + 号即可调用相同的函数。