如何在 UIViewcontroller 中向上移动模态 UIView(StoryBoard)
How to move up a modal UIView inside of a UIViewcontroller (StoryBoard)
我在 StoryBoard 创建的 UIViewcontroller 中有一个 UIView,从对象库中拖动。
在这个 UIView 里面,我放了两个 Text Field 来获取用户名和密码,还有一个按钮用来登录。
如何在出现键盘时只移动这个View?我可以移动整个 View Controller,但找不到仅移动 View 的解决方案。
我只是将对象库的一个视图拖到 Viewcontroller 中,并在 .h Viecontroller 中创建了一个 Outlet - @属性 (weak, nonatomic) IBOutlet UIView *contentView;并连接做这个视图,我也怀疑我是否需要做更多的事情来将视图添加到项目中。
提前致谢!!
您必须订阅
UIKeyboardWillShowNotification
和 UIKeyboardWillHideNotification
。然后在键盘出现时向上移动模态视图,在键盘隐藏时向下移动。
然后向上或向下动画模态视图是否显示或隐藏键盘:
上移:
[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。
我在 StoryBoard 创建的 UIViewcontroller 中有一个 UIView,从对象库中拖动。
在这个 UIView 里面,我放了两个 Text Field 来获取用户名和密码,还有一个按钮用来登录。
如何在出现键盘时只移动这个View?我可以移动整个 View Controller,但找不到仅移动 View 的解决方案。
我只是将对象库的一个视图拖到 Viewcontroller 中,并在 .h Viecontroller 中创建了一个 Outlet - @属性 (weak, nonatomic) IBOutlet UIView *contentView;并连接做这个视图,我也怀疑我是否需要做更多的事情来将视图添加到项目中。
提前致谢!!
您必须订阅
UIKeyboardWillShowNotification
和 UIKeyboardWillHideNotification
。然后在键盘出现时向上移动模态视图,在键盘隐藏时向下移动。
然后向上或向下动画模态视图是否显示或隐藏键盘:
上移:
[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。