How can I make a UITextField move up when the keyboard is present - on starting to edit?(当键盘存在时,如何使 UITextField 向上移动 - 开始编辑?)
问题描述
使用 iOS SDK:
With the iOS SDK:
我有一个带有 UITextField
的 UIView
可以调出键盘.我需要它能够:
I have a UIView
with UITextField
s that bring up a keyboard. I need it to be able to:
在打开键盘后允许滚动
UIScrollView
的内容以查看其他文本字段
Allow scrolling of the contents of the
UIScrollView
to see the other text fields once the keyboard is brought up
自动跳跃";(通过向上滚动)或缩短
Automatically "jump" (by scrolling up) or shortening
我知道我需要一个 UIScrollView
.我尝试将 UIView
的类更改为 UIScrollView
,但仍然无法向上或向下滚动文本框.
I know that I need a UIScrollView
. I've tried changing the class of my UIView
to a UIScrollView
, but I'm still unable to scroll the textboxes up or down.
我需要 UIView
和 UIScrollView
吗?一个会进入另一个吗?
Do I need both a UIView
and a UIScrollView
? Does one go inside the other?
为了自动滚动到活动文本字段需要实现什么?
What needs to be implemented in order to automatically scroll to the active text field?
理想情况下,尽可能多的组件设置将在 Interface Builder 中完成.我只想为需要的代码编写代码.
Ideally as much of the setup of the components as possible will be done in Interface Builder. I'd like to only write code for what needs it.
注意:我正在使用的 UIView
(或 UIScrollView
)是由标签栏(UITabBar
)调出的,它需要正常运行.
Note: the UIView
(or UIScrollView
) that I'm working with is brought up by a tabbar (UITabBar
), which needs to function as normal.
我只是在键盘出现时添加滚动条.尽管它不是必需的,但我觉得它提供了更好的界面,例如,用户可以滚动和更改文本框.
I am adding the scroll bar just for when the keyboard comes up. Even though it's not needed, I feel like it provides a better interface because then the user can scroll and change textboxes, for example.
当键盘上下移动时,我可以更改 UIScrollView
的帧大小.我只是在使用:
I've got it working where I change the frame size of the UIScrollView
when the keyboard goes up and down. I'm simply using:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
//Keyboard becomes visible
scrollView.frame = CGRectMake(scrollView.frame.origin.x,
scrollView.frame.origin.y,
scrollView.frame.size.width,
scrollView.frame.size.height - 215 + 50); // Resize
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
// Keyboard will hide
scrollView.frame = CGRectMake(scrollView.frame.origin.x,
scrollView.frame.origin.y,
scrollView.frame.size.width,
scrollView.frame.size.height + 215 - 50); // Resize
}
但是,这不会自动向上移动".或在可见区域中将下部文本字段居中,这是我真正想要的.
However, this doesn't automatically "move up" or center the lower text fields in the visible area, which is what I would really like.
推荐答案
如果您现在拥有的内容不适合 iPhone 屏幕,您将只需要一个
ScrollView
.(如果您添加ScrollView
作为组件的超级视图只是为了使TextField
在键盘出现时向上滚动,则不需要.)
You will only need a
ScrollView
if the contents you have now do not fit in the iPhone screen. (If you are adding theScrollView
as the superview of the components just to make theTextField
scroll up when keyboard comes up, then it's not needed.)
防止 TextField
被键盘覆盖的标准方法是在显示键盘时向上/向下移动视图.
The standard way to prevent the TextField
s from being covered by the keyboard is to move the view up/down whenever the keyboard is shown.
这里是一些示例代码:
#define kOFFSET_FOR_KEYBOARD 80.0
-(void)keyboardWillShow {
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:mailTf])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
这篇关于当键盘存在时,如何使 UITextField 向上移动 - 开始编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当键盘存在时,如何使 UITextField 向上移动 - 开始
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01