iPhone SDK:TextView,横向模式下的键盘
Posted
技术标签:
【中文标题】iPhone SDK:TextView,横向模式下的键盘【英文标题】:iPhone SDK: TextView, Keyboard in Landscape mode 【发布时间】:2010-05-14 16:39:27 【问题描述】:如何确保在横向显示文本视图并且键盘不会遮挡文本视图。
使用 UICatalog 我创建了一个可以工作的 TextViewController。其中有两种方法可以调用键盘并确保 textView 位于键盘上方。他在人像模式下效果很好。
我让横向模式工作,但 textView 仍然被放在 iPhone 的顶部,以补偿纵向模式下的键盘。
我更改了显示键盘的方法。
下面是这个方法的代码:(我只是让我们看看显示的代码,因为隐藏代码是相反的..
- (void)keyboardWillShow:(NSNotification *)aNotification
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait)
// the keyboard is showing so resize the table's height
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.height -= keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
else if (orientation == UIInterfaceOrientationLandscapeLeft)
NSLog(@"Left"); // Verijderen later
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.width -= keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
else if (orientation == UIInterfaceOrientationLandscapeRight)
NSLog(@"Right"); // verwijderen later.
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.width -= keyboardRect.size.width;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
我知道我必须更改 frame.size.height -= keyboardRect.size.height
行,但我似乎无法正常工作。
我尝试了frame.size.width -= keyboardRect.size.height
,但没有成功。丢失keyboardRect和frame一起工作,但是键盘会遮蔽textview........
【问题讨论】:
【参考方案1】:我发现上面的代码在 iPad 的横向模式下不起作用
注意:我正在移动所有字段,就我而言,这就是我所需要的 :-)
- (void)keyboardWillShow:(NSNotification*)aNotification
// Only do for Landscape Mode
if (UIInterfaceOrientationIsLandscape([self interfaceOrientation]))
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.x -= keyboardSize.height-44;
frame.origin.y -= keyboardSize.height-44;
frame.size.height += keyboardSize.height-44;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
viewMoved = YES;
keyboardInUse = YES;
- (void)keyboardWillHide:(NSNotification*)aNotification
if (UIInterfaceOrientationIsLandscape([self interfaceOrientation]))
if (viewMoved)
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.y += keyboardSize.height-44;
frame.origin.x += keyboardSize.height-44;
frame.size.height -= keyboardSize.height-44;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
viewMoved = NO;
keyboardInUse = NO;
【讨论】:
【参考方案2】:如果您认为针对不同的方向需要不同的代码,那您就做错了。一旦方向改变并且您的 superview 做出响应,需要改变的值应该始终是框架的高度。
【讨论】:
如果这是真的为什么这个代码:CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue]; NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGRect frame = self.view.frame; frame.size.height -= 键盘矩形.size.height; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = 框架; [UIView 提交动画];导致视图在横向模式下向右移动?【参考方案3】:这是我用于此目的的代码。它适用于 iPad 和 iPhone 是横向和纵向模式(灵感来自 Apple 文档中的代码):
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float offset;
if UIInterfaceOrientationIsPortrait(self.interfaceOrientation)
offset = kbSize.height;
else
offset = kbSize.width;
[UIView animateWithDuration:0.5
animations:^
CGRect frameTxtField = myTextField.frame;
frameTxtField.origin.y -= offset;
myTextField.frame = frameTxtField;
];
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float offset;
if UIInterfaceOrientationIsPortrait(self.interfaceOrientation)
offset = kbSize.height;
else
offset = kbSize.width;
[UIView animateWithDuration:0.5
animations:^
CGRect frameTxtField = myTextField.frame;
frameTxtField.origin.y += offset;
myTextField.frame = frameTxtField;
];
不要忘记调用 registerForKeyboardNotifications 方法(例如,在 viewDidLoad 中)。
【讨论】:
以上是关于iPhone SDK:TextView,横向模式下的键盘的主要内容,如果未能解决你的问题,请参考以下文章
将 MPMoviePlayerController 旋转到横向 - iPhone SDK
iPhone 6 Plus 横向模式下带有标签和固定元素的奇怪错误