iPhone SDK:TextView,横向模式下的键盘

时间:2010-05-14 16:39:27

标签: iphone-sdk-3.0 keyboard uitextview landscape

如何在风景中确保显示文本视图并且键盘不会遮挡文本视图。

使用UICatalog我创建了一个可以工作的TextViewController。在其中有两种方法可以调用键盘并确保textView位于键盘上方。他的肖像模式非常适合。

我的横向模式正常工作,但在textView上仍然被放到iPhone的顶部以补偿纵向模式下的键盘。

我改变了显示键盘的方法。

下面是这个方法的代码:(我只是看看show的代码,因为隐藏代码会反过来..

- (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和框架一起工作,但是当然键盘模糊了textview ........

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

如果您认为不同的方向需要不同的代码,那么您正在做其他错误的事情。一旦方向发生变化并且您的超级视图已响应,则需要更改的值应始终为框架的高度。

答案 2 :(得分: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中)。