[英]IOS 8 custom keyboard rotation handling to load another xib file for the landscape keyboard
我正在尋找有關處理來自iOS 8設備(iPhone 6)的旋轉通知的方法的幫助,以便為景觀鍵盤加載其他xib。
我有幾個縱向鍵盤,每個鍵盤都可以從其xib文件加載,並且都可以使用該鍵盤,但是我想為橫向布局加載另一個xib文件,但是找不到有效的解決方案。
我已嘗試按照此處許多其他答案中的建議注冊通知,但沒有成功。
我已經嘗試過這里建議的所有操作: 如何在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自定義鍵盤沒有任何作用。 任何人都知道或看到可行的解決方案會非常棒。
不要將此代碼放在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 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.