Bis
Bis

Reputation: 743

How to determine device orientation in Custom Keyboard

I am building a custom keyboard for iOS8. I want my keyboard to support portrait and landscape modes. How do I determine the device orientation? Getting device orientation doesn't currently work well in custom keyboards.

Upvotes: 0

Views: 226

Answers (3)

Jordan H
Jordan H

Reputation: 55725

willRotateToInterfaceOrientation and didRotateFromInterfaceOrientation can be used, although they are deprecated in iOS 8. Their replacement, willTransitionToTraitCollection, isn't called upon rotation though likely because the trait collection doesn't change for the keyboard.

Upvotes: 1

iOS Dev
iOS Dev

Reputation: 4248

You can use this approach:

typedef NS_ENUM(NSInteger, InterfaceOrientationType)
{
    InterfaceOrientationTypePortrait,
    InterfaceOrientationTypeLandscape
};

- (InterfaceOrientationType)orientation
{
    InterfaceOrientationType result;

    if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height)
    {
        result = InterfaceOrientationTypePortrait;
    }
    else
    {
        result = InterfaceOrientationTypeLandscape;
    }

    return result;
}

Upvotes: 0

nurnachman
nurnachman

Reputation: 4565

Unfortunately, the standard method for determining device orientation doesn't work yet in iOS8 custom keyboards.

You can determine the device orientation by checking the ratios between the width and the height of the device. height > width = portrait width > height = landscape

Upvotes: 0

Related Questions