旋转到横向时,键盘框架原点似乎是(0,0)

时间:2014-01-04 23:51:18

标签: ios iphone ipad uikeyboard cgrect

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

...谢谢

1 个答案:

答案 0 :(得分:0)

在您的方法中实施以下实施

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