【问题标题】:Keyboard frame origin appears to be (0,0) when rotating to landscape旋转到横向时,键盘框架原点似乎是 (0,0)
【发布时间】:2014-01-04 23:51:17
【问题描述】:

我试图让 UIView 出现在键盘上方,并确保它在视图旋转到横向时保持正确的定位,但是当我在旋转到横向时获得键盘的框架时,框架是: {{0, 0}, {162, 568}}。显然,原点的 Y 值不应为 0,因为键盘不在屏幕顶部。如何将它为我提供的键盘框架转换为我可以在横向中正确使用的 CGRect?

谢谢...

【问题讨论】:

    标签: ios iphone ipad uikeyboard cgrect


    【解决方案1】:

    在你的方法中实现以下实现

    if (self.view.bounds.size.width == 480)
    { // choose more appropriate method to detect landscape mode then the one specified here.
    
        /* Here we are calculating keyboard origin y by subtracting keyboard frame's width as
         * when the device is rotated to landscape, the keyboard device width and height 
         * appeared to be interchanged implicity but the keyboard's bounds still explicitly 
         * shows portrait bounds, so we are subtracting self.view.bounds size height 
         * with keyboard frame width
         */
    
        float keyboardOriginY = self.view.bounds.size.height - keyboardFrame.size.width;
        float delta = 30;  // Subtract with whatever delta value you want to position above keyboard frame
        keyboardFrame.origin.y = keyboardOriginY - delta;
        keyboardFrame.size.height = delta;
    
        UIView *view = [[UIView alloc] initWithFrame:keyboardFrame];
        [view setBackgroundColor:[UIColor greenColor]];
        [self.view addSubview:view];
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-29
      • 2015-10-27
      • 1970-01-01
      • 2015-03-18
      • 2010-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      相关资源
      最近更新 更多