我正在寻找有关处理来自iOS 8设备(iPhone 6)的旋转通知的方法的帮助,因此我可以为Landscape键盘加载不同的xib。
我有一些肖像键盘,每个键盘都从其xib文件中加载,所有这些都适用于此,但我想为横向布局加载另一个xib文件,但没有找到有效的解决方案。
我已经尝试过按照许多其他答案中的建议注册通知,但它没有用。
我已尝试过这里推荐的所有内容:How to detect Orientation Change in Custom Keyboard Extension in iOS 8?,但是通过检测推荐的尺寸来确定我们是在横向还是纵向中仍然无效。
添加一些使用的代码:
const int keyboardHeight = 375;
const int landscapekeyboardHeight = 200;
- (void)updateViewConstraints {
[super updateViewConstraints];
NSLayoutConstraint *_heightConstraintforLandscape
= [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:landscapekeyboardHeight];
NSLayoutConstraint *_heightConstraintforPortrait
= [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:keyboardHeight];
if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height)
{
// Portrait
[self.view removeConstraint: _heightConstraintforLandscape];
[self.view addConstraint: _heightConstraintforPortrait];
}
else
{
// Landscape
[self.view removeConstraint: _heightConstraintforPortrait];
[self.view addConstraint: _heightConstraintforLandscape];
self.inputView = (UIInputView*)self.LandscapeKeyboard;
}
}
我也尝试过使用方向更改通知
// Request to turn on accelerometer and begin receiving accelerometer events
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleOrientationChangeWithNotification:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
- (void)handleOrientationChangeWithNotification:(NSNotification *)notification {
// Respond to changes in device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait)
{
self.inputView = (UIInputView*)self.Keyboard;
}
else
{
self.inputView = (UIInputView*)self.LandscapeKeyboard;
}
}
我已尝试过Stack Overflow上提出的大多数解决方案,但对于iOS 8自定义键盘没有任何效果。任何知道或看到有效的解决方案的人都会非常棒。
答案 0 :(得分:0)
请勿将此代码放在 updateViewConstraints
中if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height)
{
// Portrait
[self.view removeConstraint: _heightConstraintforLandscape];
[self.view addConstraint: _heightConstraintforPortrait];
}
else
{
// Landscape
[self.view removeConstraint: _heightConstraintforPortrait];
[self.view addConstraint: _heightConstraintforLandscape];
self.inputView = (UIInputView*)self.LandscapeKeyboard;
}
而是将其移动到 viewDidLayoutSubviews ,只要设备旋转,就会调用它。