【问题标题】:How to move up a modal UIView inside of a UIViewcontroller (StoryBoard)如何在 UIViewcontroller (StoryBoard) 内向上移动模态 UIView
【发布时间】:2015-07-31 11:03:09
【问题描述】:

我在 StoryBoard 创建的 UIViewcontroller 内有一个 UIView,从 Object Library 中拖出。

在这个 UIView 里面,我放了两个 Text Field 来获取用户名和密码,以及一个按钮来登录。

当键盘出现时,我怎样才能只移动这个视图?我可以移动整个视图控制器,但我找不到只移动视图的解决方案。

我只是在我的 Viewcontroller 中拖动对象库的一个 View 并在 .h Viecontroller 中创建了一个 Outlet - @property (weak, nonatomic) IBOutlet UIView *contentView;并连接做这个视图,如果我需要做更多的事情来将视图添加到项目中,我也有疑问。

提前致谢!!

View inside UIViewController;

【问题讨论】:

标签: ios iphone uiview uiviewcontroller keyboard


【解决方案1】:

您必须订阅 UIKeyboardWillShowNotificationUIKeyboardWillHideNotification。然后当键盘出现时向上移动模态视图,当键盘隐藏时向下移动。

然后向上或向下动画模式视图,无论键盘将显示还是隐藏:

向上移动:

[UIView animateWithDuration:ANIMATION_DURATION animations:^{
    CGRect currentFrame = modalView.frame;
    currentFrame.origin.y -= VIEW_FRAME_OFFSET;
    modalView.frame = currentFrame;
}];

向下移动:

[UIView animateWithDuration:ANIMATION_DURATION animations:^{
    CGRect currentFrame = modalView.frame;
    currentFrame.origin.y += VIEW_FRAME_OFFSET;
    modalView.frame = currentFrame;
}];

如果您愿意,还可以使用here 列出的其他键盘通知。

【讨论】:

  • Uiview 只在执行此代码后快速移动并返回到键盘下方的同一点。有什么想法吗?谢谢!
猜你喜欢
  • 2014-05-02
  • 1970-01-01
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 1970-01-01
  • 2015-01-31
相关资源
最近更新 更多