![](/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.