Xcode/iOS5: Move UIView up, when keyboard appears(Xcode/iOS5:当键盘出现时,向上移动 UIView)
问题描述
我想在显示键盘时向上移动视图.键盘(高度:216)应该用它的高度推高我的视野.这可以通过简单的代码实现吗?
I'd like to move up my view, when the keyboard is shown. The keyboard (height: 216) should push up my view with it's height. Is this possible with a simple code?
推荐答案
要移动视图上
,只需改变它的center
.首先,将原始的保存在 CGPoint
属性中.
To move the view up
, just change its center
. First, keep the original one in a CGPoint
property.
- (void)viewDidLoad
{
...
self.originalCenter = self.view.center;
...
}
然后,当键盘出现时根据需要进行更改:
Then, change as needed when keyboard shows up:
self.view.center = CGPointMake(self.originalCenter.x, /* new calculated y */);
最后,在键盘隐藏时恢复:
Finally, restore it when keyboard is hidden:
self.view.center = self.originalCenter;
随意添加动画糖
知道键盘何时出现的方法不止一种.
You have more than one way to know when the keyboard appears.
观察 UIKeyboardDidShowNotification 通知.
/* register notification in any of your initWithNibName:bundle:, viewDidLoad, awakeFromNib, etc. */
{
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
...
}
- (void)keyboardDidShow:(NSNotification *)note
{
/* move your views here */
}
用 UIKeyboardDidHideNotification
做相反的事情.
-或-
实现 UITextFieldDelegate
在编辑开始/结束时检测以移动视图.
Detect when editing begin/end to move views around.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
/* keyboard is visible, move views */
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
/* resign first responder, hide keyboard, move views */
}
根据您可能需要跟踪用户正在编辑哪个字段的实际文本字段,添加一个计时器以避免过多地移动视图.
Depending on the actual text fields you may need to track in which field is the user editing, add a timer to avoid moving views too much.
这篇关于Xcode/iOS5:当键盘出现时,向上移动 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Xcode/iOS5:当键盘出现时,向上移动 UIView
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01