[英]How do I scroll in iOS when view is only bigger than screen when keyboard appears?
我是一个相当新的iPhone开发人员,我正在开发一个iPhone应用程序,其中有一个用户需要输入多个UITextViews输入的视图。 总共有6个UITextViews,当视图出现时,所有文本视图都可见而无需滚动。 但是当用户点击第一个文本视图以输入文本时,最后2个文本视图会被键盘隐藏,我无法弄清楚如何添加滚动功能,以便用户可以在键盘可见时滚动。 我正在使用UIScrollView,但目前没有代码可以使它工作,因为我尝试了多种不同的东西,我在网上找到,没有一个有效。 这可能是一个简单的解决方案,但我只是出于想法而已经被困住了一段时间。 任何意见是极大的赞赏。 谢谢
更多信息:我正在使用最新版本的Xcode,为iPhone 6.1及更高版本开发。 我使用Interface Builder设置ScrollView并选中AutoLayout框。
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
/* this returns the keyboard when user click on return button which is reside on keyboard */
if([text isEqualToString:@"\n"])
{
[textView resignFirstResponder];
[yourscrollview setContentOffset:CGPointMake(0,0)animated:YES];
}
return YES;
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
/* it used for hide keyboard and set all control on their original position */
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
/* depending upon condition it will scroll the textview so textview can't reside behind the keyboard */
[yourscrollview setContentOffset:CGPointMake(0,textView.center.y-80)animated:YES];
}
80以上是我定义的,因为我的要求是在键盘出现时采用textview,你可以放一个适合你要求的值
在你的视图中加载写下面的行
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide) name:UIKeyboardWillHideNotification object:nil];
制作以下方法。
-(void)keyboardDidHide
{
scrollview.frame = YOUR_ORIGINAL_FRAME;//You should set frame when keyboard is not there
scrollview.contentSize=scrollview.frame.size;
}
-(void)keyboardDidShow
{
CGRect r = scrollview.frame;
scrollview.contentSize=scrollview.frame.size;
r.size.height - = 216;//216 is keyboard height for iPhone.
scrollview.frame = r;
}
按照以下2个简单步骤操作
从storyboard / xib中,调整滚动视图的框架并将高度保持为屏幕大小。
在你的viewcontroller中为你的视图应用contentsize,比如
<scrollview>.contentSize=CGSizeMake(320, 700);
滚动时,您将能够看到整个滚动视图。
使用以下链接可在键盘出现时自动上下移动文本视图或文本字段
https://github.com/michaeltyson/TPKeyboardAvoiding
此链接包含演示项目。 你可以根据需要使用它
我希望这能帮到您。
你可以这样做:
// In View First add keyboard appearance and disappearance Notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
// Now inside this selector method
-(void)keyboardWillShow:(NSNotification *)notify
{
if (!notify) {
return;
}
NSDictionary *userInfo = [notify userInfo];
NSValue *keyboardEndFrame = ([userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]);
CGRect endFrame = keyboardEndFrame.CGRectValue;
// Change the frame of the view to its parent
CGRect loginViewFrame = [loginView.superview convertRect:loginView.frame fromView:loginView];
// Check the keyboard covers the view
if (CGRectGetMinY(endFrame) < CGRectGetMaxY(loginViewFrame))
{
// If YES calculate Distance. Save this difference to animate back the view
difference = CGRectGetMaxY(loginViewFrame)- CGRectGetMinY(endFrame);
// animate that View
[self animateViewUp:YES withMovementDistance:difference];
}
}
// inside Will Hide
-(void)keyboardWillHide
{
// Animate back the view with the calculated distance
[self animateViewUp:NO withMovementDistance:difference];
}
- (void)animateViewUp:(BOOL)up withMovementDistance:(int)movementDistance
{
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView animateWithDuration:movementDuration animations:^{
loginView.frame = CGRectOffset(loginView.frame, 0, movement);
}];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.