iPhone SDK:横向模式下的文本视图、键盘

发布于 2024-09-01 17:59:58 字数 2435 浏览 5 评论 0原文

如何确保在横向时显示文本视图并且键盘不会遮挡文本视图。

使用 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,但没有成功。工作,但是键盘当然会遮挡文本视图......

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........

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何止钟意 2024-09-08 17:59:59

我发现上面的代码在 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;
}

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;
}
我是男神闪亮亮 2024-09-08 17:59:59

如果您认为不同的方向需要不同的代码,那么您就做错了。一旦方向发生变化并且您的超级视图做出响应,需要更改的值应该始终是框架的高度。

If you think you need different code for the different orientations, you're doing something else wrong. Once the orientation has changed and your superview has responded, the value that needs changing should always be the frame's height.

山色无中 2024-09-08 17:59:59

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

Here is a code that I use for this purpose. It works on both iPad and iPhone is landscape and portrait modes (inspired by a code from Apple documentation):

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

Don't forget to call the registerForKeyboardNotifications method (e.g., in viewDidLoad).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文