[英]How to move view at the center of visible frame when keyboard appears?
I have a standard UIView
(containing an image view, two textfield and a button) embedded in a root scroll view. 我在根滚动视图中嵌入了一个标准的
UIView
(包含一个图像视图,两个文本字段和一个按钮)。 I'm using AutoLayout to put the UIView
at the center of the scroll view, like is shown in the screenshot below. 我正在使用AutoLayout将
UIView
放在滚动视图的中心,如下面的屏幕快照所示。
I am trying to write a method that moves up the UIView
when the keyboard appears, so that this view will appear at the center of the smaller visible frame. 我正在尝试编写一种方法,当键盘出现时,该方法将
UIView
向上移动,以便该视图将出现在较小可见框的中心。 To achieve this, I calculated some heights. 为此,我计算了一些高度。
- (void)keyboardWillShow:(NSNotification *)notification {
// Determines the size of the keyboard frame
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:0.3 animations:^{
// Gets the root 'UIView' frame and stores it in a variable
CGRect viewFrame = self.itemsView.frame;
// This is the height of the visible frame once the keyboard appears
double visibleFrameHeight = (self.view.frame.size.height - keyboardSize.height);
// How much the 'UIView' should move up
double offset = ((visibleFrameHeight - viewFrame.size.height / 2);
// Moves up the 'UIView'
viewFrame.origin.y = -offset;
self.itemsView.frame = viewFrame;
}];
}
The problem is that the UIView
is moved too up, like shown below. 问题在于
UIView
太上移,如下所示。
Why is this happening and how can I fix it? 为什么会发生这种情况,我该如何解决?
If your view is already embedded in a UIScrollView then you can just adjust the scrollview's contentOffset
to move the view (then you get the animation for free). 如果您的视图已经嵌入UIScrollView中,则只需调整滚动视图的
contentOffset
即可移动视图(然后您可以免费获得动画)。
Assuming the scrollview is available on an outlet called scrollView
, and that you want to keep the view centered, you could just shift the content offset by 1/2 the keyboard height. 假设scrollview在名为
scrollView
的插座上scrollView
,并且您想使视图居中,则只需将内容偏移量移动键盘高度的1/2。 Also - probably safer to use the UIKeyboardFrameEndUserInfoKey
to get the final frame of the keyboard: 另外-使用
UIKeyboardFrameEndUserInfoKey
来获取键盘的最后一帧可能更安全:
- (void)keyboardWillShow:(NSNotification *)notification {
// Determines the size of the keyboard frame
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
self.scrollView.contentOffset = CGPointMake(0, keyboardSize.height / 2);
}
- (void)keyboardWillHide:(NSNotification *)notification {
self.scrollView.contentOffset = CGPointZero;
}
you have to set one IBOutlet for UIScrollView in .h file like this: IBOutlet UIScrollView *scrLogin; 您必须像这样在.h文件中为UIScrollView设置一个IBOutlet: IBOutlet UIScrollView * scrLogin;
then after in Your xib file set delegate for UIScrollview and all UiTextfield. 然后在您的xib文件中为UIScrollview和所有UiTextfield设置委托 。
then after set this code in your .m file 然后在您的.m文件中设置此代码后
You have set all design like UIimageview, UItextfield, & UIButton & every thing in UIScrollView. 您已经设置了所有设计,如UIimageview,UItextfield和UIButton以及UIScrollView中的所有内容。
- (void)viewDidLoad {
[scrLogin setContentSize:CGSizeMake(320, 790)];
[scrLogin setContentOffset:CGPointMake(0, 0) animated:YES];
}
#pragma mark - Textfield Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField==txtUserName) {
[txtUserName resignFirstResponder];
[txtPsw becomeFirstResponder];
}else{
[txtPsw resignFirstResponder];
[scrLogin setContentOffset:CGPointMake(0, -60) animated:NO];
//[self btnLoginTapped:self];
}
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField==txtUserName){
if (IS_IPHONE_4s) {
[scrLogin setContentOffset:CGPointMake(0, 20) animated:true];
}else{
[scrLogin setContentOffset:CGPointMake(0, 0) animated:true];
}
}else if(textField==txtPsw){
[scrLogin setContentOffset:CGPointMake(0, 40) animated:true];
}
} }
I hope it's help you. 希望对您有帮助。 if you have any problem then tell me.
如果您有任何问题,请告诉我。
我建议使用https://github.com/hackiftekhar/IQKeyboardManager ,它易于使用并且可在您的整个应用程序中使用(无需再次手动计算键盘高度)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.