iphone - 键盘出现时 Xcode/iOS5 : Move UIView up,

标签 iphone ios uiviewcontroller keyboard ios5

当显示键盘时,我想向上移动我的 View 。键盘(高度:216)应该用它的高度推高我的视野。这可以用简单的代码实现吗?

最佳答案

向上移动 View ,只需改变它的中心。首先,将原始的保存在 CGPoint 属性中。

- (void)viewDidLoad 
{
    ...
    self.originalCenter = self.view.center;
    ...
}

然后,在键盘出现时根据需要进行更改:

self.view.center = CGPointMake(self.originalCenter.x, /* new calculated y */);

最后,当键盘隐藏时恢复它:

self.view.center = self.originalCenter;

动画糖随意加

您有不止一种方法可以知道键盘何时出现。

观察 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

在编辑开始/结束时检测以移动 View 。

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{
    /* keyboard is visible, move views */
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    /* resign first responder, hide keyboard, move views */
}

根据您可能需要跟踪用户正在编辑哪个字段的实际文本字段,添加一个计时器以避免过多地移动 View 。

关于iphone - 键盘出现时 Xcode/iOS5 : Move UIView up,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7952762/

相关文章:

iphone - 《水果忍者》游戏中的切片效果如何发挥作用?

ios - 核心动画 - 未调用 animationDidStop

iphone - ios 7 tableView didSelectRowAtIndexPath 在第一次选择时显示空白数据

ios - 在iOS中为Unicode值显示错误的字符

ios - 移动到下一个 UIViewController 时保留 UINavigationController 但隐藏 UITabBarController

iphone - 如何将 IBAction 事件与图像相关联?

ios - 捕获 iOS 3D Touch 光标

iphone - 重新创建 iOS 相机应用覆盖按钮

swift - 如何在两个 View Controller 之间传递(变化的)变量?

ios - 如何在不使用单例的情况下在多个 View Controller 之间传递数据?