[英]Several UITextFields in a UIScrollView, covered by the keyboard
我在UIScrollView
有几个UITextFields
。 当我编辑其中一个并且弹出键盘时,该字段被键盘覆盖。
我想向上滚动视图,我认为这是iOS自动完成的,但事实并非如此。
我目前正在使用此方法滚动视图,但是效果不是很好。
- (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];
}
谢谢
就我而言,我要做的是减小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];
}
试试这个示例代码TPKeyboardAvoiding
它易于实现。 只需拖放自定义类,然后将TPKeyboardAvoidingScrollView
中的Custom类从UIScrollview TPKeyboardAvoidingScrollView
为TPKeyboardAvoidingScrollView
//注册键盘通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
在这两种方法中设置滚动视图的框架..当键盘显示或隐藏时。
或设置滚动视图的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];
}
在textfield或textview中调用此方法开始编辑。它将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.