gpt4 book ai didi

iphone - 从横向切换到纵向后,将 UIScrollView 保持在正确的位置

转载 作者:行者123 更新时间:2023-11-29 10:56:22 24 4
gpt4 key购买 nike

我有一个包含文本字段和 TextView 的 UIScrollView。当键盘存在时,我有代码将文本字段向上移动,这样键盘就不会覆盖文本字段。此代码在纵向 View 中效果很好:

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if(textField) {
[textField resignFirstResponder];
}
return NO;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == self.nameField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 90), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == self.nameField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 90), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}

-(void)textViewDidBeginEditing:(UITextView *)textField {
if (textField == self.questionField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 200), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}

-(void)textViewShouldEndEditing:(UITextView *)textField {
if (textField == self.questionField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;

self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 200), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}

当我将 iPhone 模拟器旋转到横向 View 时,这(不足为奇)不起作用。在横向和纵向 View 中输入文本时,如何让文本字段向上移动到足以看到它的程度?

此外,如果我在显示键盘时从横向旋转到纵向,则在关闭键盘后, ScrollView 已向下移动到屏幕,而不是在其原始位置排列。我怎样才能避免这种情况?

最佳答案

苹果文档Managing the Keyboard在“移动位于键盘下方的内容”标题下显示了执行此操作的正确方法。

您基本上是将内容放在 ScrollView 上,并使用 UIKeyboardDidShowNotificationUIKeyboardWillHideNotification 通知来调整内容偏移量。

但是,代码有些不完整。键盘尺寸计算在横向模式下不起作用。要修复它,请替换为:

CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

有了这个:

// Works in both portrait and landscape mode
CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
kbRect = [self.view convertRect:kbRect toView:nil];

CGSize kbSize = kbRect.size;

关于iphone - 从横向切换到纵向后,将 UIScrollView 保持在正确的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18110335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com