我正在为 iOS8 构建一个自定义键盘。我希望我的键盘支持纵向和横向模式。如何确定设备方向?获取设备方向目前在自定义键盘中效果不佳。
3 回答
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.
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
您可以使用这种方法:
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;
}