[英]Detect rotation in UIInputViewController subclass. Keyboard extension
[英]Catch rotation event into UIInputViewController
我正在开发自定义键盘,并且试图捕获设备旋转事件。
我已经尝试过了:
override func viewDidLayoutSubviews() {
print("Foo")
}
和
override func viewWillLayoutSubviews() {
print("🐳🐳🐳🐳🐳🐳🐳🐳🐳🐳🐳🐳 Bar")
}
和
override func didRotateFromInterfaceOrientation(sender : UIInterfaceOrientation){
print("🐳🐳🐳🐳");
}
和
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
print("Bar")
}
和
override func updateViewConstraints() {
super.updateViewConstraints()
print("🐳🐳🐳🐳🐳🐳🐳🐳🐳🐳🐳🐳 Foo")
}
但这不起作用。
iPad Air 2模拟器-iOS9
任何想法 ?
现在不推荐使用大多数与旋转有关的方法。 苹果正朝着尺寸和特征更改通知的方向发展,而不是直接面向设备方向的更改通知。
同样,在具有完全访问权限和beginGeneratingDeviceOrientationNotifications的键盘扩展中,UIDevice.currentDevice()。orientation始终返回Unknown。
正确的方法(以及替代文档中不推荐使用的方法的方法)是
- viewWillTransitionToSize:withTransitionCoordinator:
奇怪的是它对您不起作用。 我只是在iPad Air 2 iOS9 Simulator的UIInputViewController中尝试过,它的工作原理是:
2015-10-13 12:28:15.347 [Info] [KeyboardViewController.swift:60] viewWillTransitionToSize( :withTransitionCoordinator :)>旋转至大小(1024.0,313.0)2015-10-13 12:28:20.512 [Info] [KeyboardViewController .swift:60] viewWillTransitionToSize( :withTransitionCoordinator :)>旋转至大小(768.0,313.0)
您是否在调试正确的目标? 要查看扩展中的控制台消息,必须将其设置为运行目标:
请注意,还有
- willTransitionToTraitCollection:withTransitionCoordinator:
但这不适用于iPad,因为iPad始终对风景和人像都是“常规”,“常规”。
并非所有的视图控制器都将接收过渡事件。 我认为这取决于您的视图控制器是否在UINavigationController或UITabBarController之类的容器控制器内部,以及它们是否将更改传递给其子控制器。
接收方向更改的保证方法是注册UIDevice方向更改通知。 有关通知事件的列表,请参见https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/#//apple_ref/c/data/UIDeviceOrientationDidChangeNotification 。
这是您在ObjC中要做的事情。 应该足够容易地转换为Swift。
在您的应用程序委托中,使用以下方法激活方向更改:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
在视图控制器中,您希望通知添加方法来处理通知,添加和删除观察通知。
// Method to be called when orientation notification is changed.
- (void)orientationDidChange:(NSNotification *)notification {
// Add your code to deal with the orientation.
// You can get the current orientation for UIDevice as follows:
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
}
// Adds observation of orientation change
-(void)addObserver
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationDidChange:)
name:@"UIDeviceOrientationDidChangeNotification"
object:nil];
}
// Removes observation of orientation change
-(void)removeObserver
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
在适当的位置添加对addObserver
和removeObserver
调用。 例如, viewDidAppear
和viewDidDisappear
。
看来这在我的iOS 9和iPad Air 2项目中适用。
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
print("hi")
}
看起来您尝试过,但是却没有效果...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.