[英]Several UITextFields in a UIScrollView, covered by the keyboard
I have several UITextFields
in a UIScrollView
. 我在UIScrollView
有几个UITextFields
。 When I edit one of them and the keyboard pops-up, the field is covered by the keyboard. 当我编辑其中一个并且弹出键盘时,该字段被键盘覆盖。
I would like to scroll up the view, I thought this was automatically done by iOS but it seems not to be the case. 我想向上滚动视图,我认为这是iOS自动完成的,但事实并非如此。
I'm currently using this method to scroll the view, but it doesn't work very well. 我目前正在使用此方法滚动视图,但是效果不是很好。
- (void)scrollToView:(UIView *)view
{
CGRect viewFrame = [[edit scrollView] convertRect:[view frame] fromView:[view superview]];
CGRect finalFrame = CGRectMake(viewFrame.origin.x, viewFrame.origin.y, viewFrame.size.width, (viewFrame.size.height + (inputAccessory.frame.size.height) + 4.0));
[[edit scrollView] scrollRectToVisible:finalFrame animated:YES];
}
thanks 谢谢
In my case, what I do is reduce the size of the scrollView
in the Editing Did Begin
event of the UITextField
, like this: 就我而言,我要做的是减小UITextField
的scrollView
Editing Did Begin
事件中的scrollView
的大小,如下所示:
- (IBAction)didEnterInTextField:(id)sender
{
[sender becomeFirstResponder];
// Resize the scroll view to reduce the keyboard height
CGRect scrollViewFrame = self.scrollView.frame;
if (scrollViewFrame.size.height > 300) {
scrollViewFrame.size.height -= 216;
self.scrollView.frame = scrollViewFrame;
}
// Scroll the view to see the text field
UITextField *selectedTextField = (UITextField *)sender;
float yPosition = selectedTextField.frame.origin.y - 60;
float bottomPositon = self.scrollView.contentSize.height - self.scrollView.bounds.size.height;
if (yPosition > bottomPositon) {
yPosition = bottomPositon;
}
[self.scrollView setContentOffset:CGPointMake(0, yPosition) animated:YES];
}
Here is nice tutorial on same 这是一个很好的教程
http://www.iphonesampleapps.com/2009/12/adjust-uitextfield-hidden-behind-keyboard-with-uiscrollview/ http://www.iphonesampleapps.com/2009/12/adjust-uitextfield-hidden-behind-keyboard-with-uiscrollview/
Hope it helps you 希望对您有帮助
Try this sample code TPKeyboardAvoiding 试试这个示例代码TPKeyboardAvoiding
Its easy to implement. 它易于实现。 Just drag and drog the custom classes and change the Custom class from UIScrollview to TPKeyboardAvoidingScrollView
in the xib 只需拖放自定义类,然后将TPKeyboardAvoidingScrollView
中的Custom类从UIScrollview TPKeyboardAvoidingScrollView
为TPKeyboardAvoidingScrollView
// register for keyboard notifications //注册键盘通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
set the frame of scroll view in these two methods.. when keyboards shows or hides. 在这两种方法中设置滚动视图的框架..当键盘显示或隐藏时。
or set the scrollRectToVisible of scroll view 或设置滚动视图的scrollRectToVisible
- (void)scrollViewToCenterOfScreen:(UIView *)theView
{
CGFloat viewCenterY = theView.center.y;
CGFloat availableHeight;
CGFloat y;
if(!isReturned)
{
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
availableHeight = 1080;
}
else
{
availableHeight = 220;
}
}
else
{
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
availableHeight = 1400;
}
else
{
availableHeight = 530;
}
}
y = viewCenterY - availableHeight / 2.0;
if (y < 0) {
y = 0;
}
[sclView setContentOffset:CGPointMake(0, y) animated:YES];
}
Call this method in textfield or textview begin editing.It will works. 在textfield或textview中调用此方法开始编辑。它将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.