Xcode/iOS5:当键盘出现时向上移动 UIView [英] Xcode/iOS5: Move UIView up, when keyboard appears

查看:27
本文介绍了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?

推荐答案

要移动视图 up,只需更改其 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 做相反的事情.

Do the opposite with 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆