![](/img/trans.png)
[英]Can't use UICollectionView in UIInputViewController for keyboard extension
[英]Detect rotation in UIInputViewController subclass. Keyboard extension
我一直在開發自定義鍵盤擴展,並且在設備旋轉后需要以編程方式更新一些約束。 我一直在嘗試在UIInputViewController子類中檢測用戶界面旋轉,但沒有成功。 設備旋轉時不會調用以下方法:
-(void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>) coordinator { NSLog(@"Orientation changed"); }
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { NSLog(@"Orientation changed"); }
我也嘗試觀察UIDeviceOrientationDidChangeNotification
但它也不起作用。
有人知道如何在UIInputViewController
檢測旋轉嗎?
您是正確的-鍵盤擴展的UIInputViewController子類中未調用這些方法(不確定其他擴展類型)。
您需要覆蓋viewDidLayoutSubviews
。
例如,如果您想在設備旋轉時更新鍵盤的UICollectionView子視圖的布局,則可以執行以下操作:
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[self.keyboard.collectionView performBatchUpdates:nil completion:nil];
}
您可以在viewDidLayoutSubviews中檢查設備的方向,然后適當地應用/修改約束。
看來Apple沒有為UIInputViewController提供此API方法。
如果設備寬度大於viewWillAppear
設備高度,則可以推斷設備方向
斯威夫特4.2
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
let screen = UIScreen.main.bounds
if screen.width < screen.height {
print("!!! portrait")
} else {
print("!!! landspace")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.