【问题标题】:How do I create a UITextView that exactly fits above the keyboard?如何创建一个完全适合键盘上方的 UITextView?
【发布时间】:2012-03-09 07:44:44
【问题描述】:

我想创建一个视图,例如 Facebook 或 Twitter 应用程序的共享对话框,其中只有一个 UITextView 和一个永久键盘。我可以看到如何从this answer 可见的键盘开始,但我不确定如何调整 UITextView 的大小以恰好适合键盘上方。如果我不这样做,文本可能会隐藏在键盘下,这很尴尬。

【问题讨论】:

    标签: iphone ios cocoa-touch ios5


    【解决方案1】:

    我发现这个页面总是很有帮助 - 它显示了所有重要的尺寸,所以很容易计算出你需要的尺寸:

    http://www.idev101.com/code/User_Interface/sizes.html

    【讨论】:

    • 并非所有键盘的尺寸都相同。以“简体中文(笔画)”为例。
    【解决方案2】:

    您可以订阅UIKeyboardWillShowNotification。通知包括键盘框架,因此您可以调整视图大小以适应剩余空间。

    【讨论】:

      【解决方案3】:

      你可以像这样得到键盘尺寸:

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardIsUp:) name:UIKeyboardDidShowNotification object:nil];
      
      - (void)keyboardIsUp:(NSNotification *)notification{
      
          CGSize keyboardSize = [self.view convertRect:[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:nil].size;
          NSLog(@"%f", keyboardSize.height);
      }
      

      [编辑]横向模式

      我在 iPas Simulator 上尝试过,它在纵向模式下为 QWERTY 键盘返回 264,但是当您启动应用程序或旋转到横向模式时,它返回 1024。所以您可能需要询问宽度而不是高度横向模式...

      [编辑]

      感谢 rob mayoff 的评论,横向模式已经没有问题了

      [编辑]

      这不是最好的方法,但它提供了一个想法。我稍后再看一遍

      - (void)viewDidLoad
      {
          [super viewDidLoad];
      
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
      
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
      
          CGSize size = [self.view convertRect:self.view.frame toView:nil].size;
          CGFloat width =  size.width;
          CGFloat height = 40;
          CGFloat x =  0;
          CGFloat y =  size.height+40;
      
          aboveKBView = [[[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)] autorelease];
          [aboveKBView setBackgroundColor:[UIColor greenColor]];
          [self.view addSubview:aboveKBView];
      }
      
      - (void)keyboardWillHide:(NSNotification *)notification{
          [aboveKBView setHidden:YES];
      }
      
      - (void)keyboardWillShow:(NSNotification *)notification{
          NSLog(@"keyboardIsUp");
      
          CGSize keyboardSize = [self.view convertRect:[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:nil].size;
      
          CGSize size = [self.view convertRect:self.view.frame toView:nil].size;
          CGFloat width =  size.width;
          CGFloat height = 40;
          CGFloat x =  0;
          CGFloat y =  size.height-(keyboardSize.height+height);
      
          [aboveKBView setFrame:CGRectMake(x, y, width, height)];
          [aboveKBView setHidden:NO];    
      }
      

      【讨论】:

      • 通知中的框架是全局坐标。您需要使用 [myView convertRect:keyboardFrame fromView:nil] 之类的东西将其转换为适当的坐标系。
      • 这适用于获取大小,但我仍然无法将 UITextView 设置为正确的高度。在下面的代码中,frame.size.heigh 是 367,keyboardHeight 是 216,但由于某种原因,当我调用 setFrame 时,文本视图和键盘之间存在空间。仅供参考,我正在设置观察者并在viewWillAppear 中调用[self.textView becomeFirstResponder]
      • ` float keyboardHeight = [self.view convertRect:[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:nil].size.height; CGRect frame = self.view.frame; [self.textView setFrame: CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height - keyboardHeight)]; `
      【解决方案4】:

      试试这个

      在 viewDidLoad 中:

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
      
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
      

      并添加这些方法。

      - (void) keyboardWillShow: (NSNotification*) aNotification;
      {   
      
          [UIView beginAnimations:nil context:NULL];
      
          [UIView setAnimationDuration:0.3];
      
          CGRect rect = [[self view] frame];
      
          rect.origin.y -= 60; 
      
          [[self view] setFrame: rect];
      
          [UIView commitAnimations];
      
      }
      
      - (void) keyboardWillHide: (NSNotification*) aNotification;
      {
      
          [UIView beginAnimations:nil context:NULL];
      
          [UIView setAnimationDuration:0.3];
      
          CGRect rect = [[self view] frame];
      
          rect.origin.y += 60; 
      
          [[self view] setFrame: rect];
      
          [UIView commitAnimations];
      }
      

      【讨论】:

        【解决方案5】:

        我在 Apple 文档中找到了一个有用的代码示例:https://developer.apple.com/library/ios/#samplecode/KeyboardAccessory/Introduction/Intro.html

        这是我最终得到的代码。我不担心键盘隐藏,因为在我看来,键盘永远不应该隐藏。

        - (void)viewWillAppear:(BOOL)flag
        {
            [super viewWillAppear:flag];
        
            // Listen for the keyboard to show up so we can get its height
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        
            // Set focus on the text view to display the keyboard immediately
            [self.textView becomeFirstResponder];
        }
        - (void)keyboardWillShow:(NSNotification *)notification
        {
            /*
             Reduce the size of the text view so that it's not obscured by the keyboard.
             */
        
            NSDictionary *userInfo = [notification userInfo];
        
            // Get the origin of the keyboard when it's displayed.
            NSValue* keyboardFrame = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        
            // Get the top of the keyboard as the y coordinate of its origin in self's view's
            // coordinate system. The bottom of the text view's frame should align with the
            // top of the keyboard's final position.
            CGRect keyboardRect = [keyboardFrame CGRectValue];
            keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
        
            // Set the text view's frame height as the distance from the top of the view bounds
            // to the top of the keyboard
            CGFloat keyboardTop = keyboardRect.origin.y;
            CGRect newTextViewFrame = self.view.bounds;
            newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
            self.textView.frame = newTextViewFrame;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-04
          • 2011-12-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多