Ios8 如何在iOS 8中检测自定义键盘扩展中的方向变化?
在自定义键盘扩展中,我们无法使用Ios8 如何在iOS 8中检测自定义键盘扩展中的方向变化?,ios8,ios-app-extension,custom-keyboard,uiinputviewcontroller,Ios8,Ios App Extension,Custom Keyboard,Uiinputviewcontroller,在自定义键盘扩展中,我们无法使用 `didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation` 和sharedApplication 旋转时,我需要在键盘上检测纵向或横向 如何检测自定义键盘扩展插件中的方向何时更改?要在方向更改时更新自定义键盘,请覆盖UIInputViewController中的ViewDidLayoutSubView。据我所知,当旋转发生时,总是调用此方法 此外,由
`didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation`
和sharedApplication
旋转时,我需要在键盘上检测纵向或横向
如何检测自定义键盘扩展插件中的方向何时更改?要在方向更改时更新自定义键盘,请覆盖
UIInputViewController
中的ViewDidLayoutSubView
。据我所知,当旋转发生时,总是调用此方法
此外,由于传统的[UIApplication sharedApplication]statusBarOrientation]
不起作用,要确定当前方向,请使用以下代码段:
if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height){
//Keyboard is in Portrait
}
else{
//Keyboard is in Landscape
}
if([UIScreen mainScreen].bounds.size.width<[UIScreen mainScreen].bounds.size.height){
//键盘是纵向的
}
否则{
//键盘是横向的
}
希望这有帮助 使用
viewwilltransitionosize:(CGSize)size with transitionocordinator:
在viewController内部未弃用,可用于任何设备屏幕大小(包括未来的屏幕大小) 在CustomKeyboard
ViewController.m
中:
-(void)ViewDidLayoutSubView{
NSLog(@“%@”,(self.view.frame.size.width==([[UIScreen mainScreen]边界].size.width*([[UIScreen mainScreen]边界].size.width[[UIScreen mainScreen]边界].size.height))?@“纵向”:@“横向”);
}
完成了
或者。。。有关此代码更易于阅读的版本:
-(void)ViewDidLayoutSubView{
int appExtensionWidth=(int)round(self.view.frame.size.width);
int可能ScreenWidthValue1=(int)舍入([[UIScreen mainScreen]界限].size.width);
int可能ScreenWidthValue2=(int)圆形([[UIScreen mainScreen]边界].size.height);
int屏幕宽度值;
如果(可能的屏幕宽度值1<可能的屏幕宽度值2){
屏幕宽度值=可能的屏幕宽度值1;
}否则{
屏幕宽度值=可能的屏幕宽度值2;
}
if(appExtensionWidth==屏幕宽度值){
NSLog(“肖像”);
}否则{
NSLog(“景观”);
}
}
-(void)视图将转换大小:(CGSize)带有转换协调器的大小:(id)协调器
{
[协调员AnimateLongsideTransition:^(id上下文)
{UIInterfaceOrientation方向=[[UIApplication sharedApplication]statusBarOrientation];//做任何事情
}完成:^(id上下文){}];[super-viewwillTransitionOnSize:size with-TransitionOnCoordinator:coordinator];
}
有一种简单的方法,只需查看屏幕宽度即可:
double width = [[UIScreen mainScreen] bounds].size.width;
double interfaceWidth = MIN([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
BOOL isPortrait = (width == interfaceWidth) ? YES : NO;
-(void)updateViewConstraints{
[super updateViewConstraints];
//在此处添加自定义视图大小限制
if(self.view.frame.size.width==0 | | self.view.frame.size.height==0)
返回;
[self.inputView removeConstraint:self.heightConstraint];
CGSize screenSize=[[UIScreen mainScreen]界限].size;
CGFloat screenH=屏幕尺寸。高度;
CGFloat screenW=屏幕大小.WITH;
布尔岛景观=!(self.view.frame.size.width)==
(screenW*(screenWscreenH));
NSLog(isLandscape?@“屏幕:景观”:“屏幕:Potriaint”);
self.isLandscape=isLandscape;
如果(isLandscape){
self.heightConstraint.constant=self.landscapeHeight;
[self.inputView addConstraint:self.heightConstraint];
}否则{
self.heightConstraint.constant=self.height;
[self.inputView addConstraint:self.heightConstraint];
}
//触发默认第一视图
[btn_gif发送控制事件的操作:UIControlEventTouchUpInside];
}
将使用RotateToInterfaceOrientation
和didRotateFromInterfaceOrientation
,我刚刚在iOS 8.1上运行键盘的iPad上试用过。请注意,这些在iOS 8中已被弃用,但不会调用它们的替代品,WilltTransitionTraitCollection
,尽管可能是因为键盘在旋转时特征集合不会发生变化。在iOS 8.3之前,您必须使用
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
这一个不起作用(它应该是一个bug):
已弃用。在某些情况下[UIScreen mainscreen]。边界可能不起作用。有时,它会在ViewWillTransitionOnToSize之后更新:
试试这个
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat realScreenHeight = MAX(screenSize.height, screenSize.width);
if(size.width == realScreenHeight)
NSLog(@"Landscape");
else
NSLog(@"Portrait");
}
-(void)视图将转换大小:(CGSize)带有转换协调器的大小:(id)协调器{
CGSize screenSize=[[UIScreen mainScreen]界限].size;
CGFloat realScreenHeight=最大值(screenSize.height,screenSize.width);
if(size.width==真实屏幕高度)
NSLog(“景观”);
其他的
NSLog(“肖像”);
}
适用于在Swift 5中搜索答案的用户。
通过重写func didRotate(来自InterfaceOrientation:UIInterfaceOrientation)
方法。您可以检测设备方向
这是我自定义键盘的代码
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
let screen = UIScreen.main.bounds
if screen.width < screen.height {
print("!!! portrait")
let constraintForHeight:NSLayoutConstraint = NSLayoutConstraint(item: mainView!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 0, constant: 325)
constraintForHeight.isActive = true
constraintForHeight.priority = UILayoutPriority.defaultHigh
self.inputView?.addConstraint(constraintForHeight)
} else {
print("!!! landspace")
let constraintForHeight:NSLayoutConstraint = NSLayoutConstraint(item: mainView!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 0, constant: 210)
constraintForHeight.isActive = true
constraintForHeight.priority = UILayoutPriority.defaultHigh
self.inputView?.addConstraint(constraintForHeight)
}
}
重写函数didRotate(来自fromInterfaceOrientation:UIInterfaceOrientation){
let screen=UIScreen.main.bounds
如果屏幕宽度<屏幕高度{
打印(!!!肖像)
让constraintForHeight:NSLayoutConstraint=NSLayoutConstraint(项:mainView!,属性:NSLayoutConstraint.attribute.height,relatedBy:NSLayoutConstraint.Relation.equal,toItem:nil,属性:NSLayoutConstraint.attribute.NotaAttribute,乘数:0,常数:325)
constraintForHeight.isActive=true
constraintForHeight.priority=UILayoutPriority.defaultHigh
self.inputView?.addConstraint(constraintForHeight)
}否则{
打印(!!!陆地空间)
让constraintForHeight:NSLayoutConstraint=NSLayoutConstraint(项:mainView!,属性:NSLayoutConstraint.attribute.height,relatedBy:NSLayoutConstraint.Relation.equal,toItem:nil,属性:NSLayoutConstraint.attribute.NotaAttribute,乘数:
-(void)viewWillTransitionToSize:(CGSize)size
withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
-(void)viewWillTransitionToSize:(CGSize)size
withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat realScreenHeight = MAX(screenSize.height, screenSize.width);
if(size.width == realScreenHeight)
NSLog(@"Landscape");
else
NSLog(@"Portrait");
}
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
let screen = UIScreen.main.bounds
if screen.width < screen.height {
print("!!! portrait")
let constraintForHeight:NSLayoutConstraint = NSLayoutConstraint(item: mainView!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 0, constant: 325)
constraintForHeight.isActive = true
constraintForHeight.priority = UILayoutPriority.defaultHigh
self.inputView?.addConstraint(constraintForHeight)
} else {
print("!!! landspace")
let constraintForHeight:NSLayoutConstraint = NSLayoutConstraint(item: mainView!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 0, constant: 210)
constraintForHeight.isActive = true
constraintForHeight.priority = UILayoutPriority.defaultHigh
self.inputView?.addConstraint(constraintForHeight)
}
}