0

How do I make sure that the textview is shown and the keyboard is not obscuring the textview, while in landscape.

Using UICatalog I created a TextViewController which works. In it there are two methods for calling the keyboard and making sure that textView is positioned above the keyboard. his just works great in Portrait mode.

I got the Landscape mode working, but on the textView is still being put to the top of the iPhone to compensate for the keyboard in portrait mode.

I changed the methods for showing the keyboards.

Below is the code for this methods: (I will just let see the code for show, since the hide code will be the reverse..

- (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];
 }

}

I know that I have to change the line frame.size.height -= keyboardRect.size.height but I do not seem to get it working.

I tried frame.size.width -= keyboardRect.size.height that did not work. Losing the keyboardRect and frame all together work, however off course the keyboard obscures the textview........

4

3 回答 3

1

I found the above code wouldn't work when in Landscape Mode on iPad

Note: I am moving all Fields, as in my case that's what i needed :-)

- (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;
}
于 2011-05-25T21:31:17.763 回答
0

如果您认为您需要针对不同方向使用不同的代码,那么您做错了其他事情。一旦方向改变并且你的超级视图已经响应,需要改变的值应该总是框架的高度。

于 2010-06-13T20:05:26.183 回答
0

这是我用于此目的的代码。它适用于 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 中)。

于 2011-12-15T19:59:43.703 回答