Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
处理在旋转时更改iOS键盘布局_Ios_Objective C_Ios App Extension - Fatal编程技术网

处理在旋转时更改iOS键盘布局

处理在旋转时更改iOS键盘布局,ios,objective-c,ios-app-extension,Ios,Objective C,Ios App Extension,根据设备的方向,我的键盘有两种稍微不同的布局。当我开始加载键盘时,它看起来很好。以下是肖像版本: 初始约束设置发生在-viewDidLoad(UIInputViewController子类的)中 tractiveconstraints=[self-constraintsForOrientation:UIInterfaceOrientationGrative]; 景观约束=[self Constraints ForOrientation:UIInterfaceOrientation和景观右];

根据设备的方向,我的键盘有两种稍微不同的布局。当我开始加载键盘时,它看起来很好。以下是肖像版本:

初始约束设置发生在-viewDidLoad(UIInputViewController子类的)中

tractiveconstraints=[self-constraintsForOrientation:UIInterfaceOrientationGrative];
景观约束=[self Constraints ForOrientation:UIInterfaceOrientation和景观右];
[self.view addConstraints:约束];
[self.view addConstraints:landscapeConstraints];
如果([UIScreen mainScreen].bounds.size.width<[UIScreen mainScreen].bounds.size.height){
[NSLayoutConstraint停用约束:景观约束];
}否则{
[NSLayoutConstraint停用约束:约束];
}
无论初始方向如何,此零件都可以正常加载。然而,一旦我旋转设备,一切都会出错

然后回到肖像:

作为参考,正确的横向版本如下所示:

我将-updateViewConstraints中的约束更新如下:

- (void)updateViewConstraints {
    if ([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height) {
        [NSLayoutConstraint deactivateConstraints:landscapeConstraints];
        [NSLayoutConstraint activateConstraints:portraitConstraints];
    } else {
        [NSLayoutConstraint deactivateConstraints:portraitConstraints];
        [NSLayoutConstraint activateConstraints:landscapeConstraints];
    }

    [super updateViewConstraints];
}
-(void)updateViewConstraints{
如果([UIScreen mainScreen].bounds.size.width<[UIScreen mainScreen].bounds.size.height){
[NSLayoutConstraint停用约束:景观约束];
[NSLayoutConstraint activateConstraints:SOURATECONSTRAINTS];
}否则{
[NSLayoutConstraint停用约束:约束];
[NSLayoutConstraint ACTIVATECONSTRAITS:景观约束];
}
[super updateViewConstraints];
}

看起来在视图改变后,它突然占据了超过全屏的空间,而不仅仅是键盘区域。有没有办法解决这个问题?谢谢。

这个问题似乎与以某种方式隐式覆盖包含键盘的视图的高度有关。虽然我没有看到任何会强制调整容器视图大小的内容,但显式设置容器视图的高度可以得到良好的、可复制的结果

我想到了手动限制键盘的高度

具体而言,相关信息如下:

可以使用“自动布局”调整自定义键盘主视图的高度。默认情况下,自定义键盘的大小根据屏幕大小和设备方向与系统键盘匹配。自定义键盘的宽度始终由系统设置为等于当前屏幕宽度。要调整自定义键盘的高度,请更改其主视图的高度约束

以下代码行显示了如何定义和添加此类约束:

注意

在iOS 8.0中,您可以在自定义键盘的主视图最初在屏幕上绘制后随时调整其高度

以下是基于此信息的模板答案中的重要方法:

- (void)updateViewConstraints {
    if (self.keyboardHeightConstraint == nil) {
        // Just starting with SOME value for the height
        self.keyboardHeightConstraint =
        [NSLayoutConstraint constraintWithItem:self.view
                                     attribute:NSLayoutAttributeHeight
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:nil
                                     attribute:NSLayoutAttributeNotAnAttribute
                                    multiplier:0.0f
                                      constant:500.0f];
        [self.view addConstraint:self.keyboardHeightConstraint];
    }
    // Obviously, these values will be changed based on device AND orientation
    // These are bogus values...
    if ([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height) {
        self.keyboardHeightConstraint.constant  = 300.0f;
        [NSLayoutConstraint deactivateConstraints:self.landscapeConstraints];
        [NSLayoutConstraint activateConstraints:self.portraitConstraints];
    } else {
        self.keyboardHeightConstraint.constant  = 400.0f;
        [NSLayoutConstraint deactivateConstraints:self.portraitConstraints];
        [NSLayoutConstraint activateConstraints:self.landscapeConstraints];
    }
    [super updateViewConstraints];
}
-(void)updateViewConstraints{
if(self.keyboardHeightConstraint==nil){
//从高度的一些值开始
自键盘高度约束=
[NSLayoutConstraint约束项:self.view
属性:NSLayoutAttributeHeight
关系人:NSLayoutRelationEqual
toItem:无
属性:nsLayoutAttribute属性
乘数:0.0f
常数:500.0f];
[self.view addConstraint:self.keyboardHeightConstraint];
}
//显然,这些值将根据设备和方向进行更改
//这些都是虚假的价值观。。。
如果([UIScreen mainScreen].bounds.size.width<[UIScreen mainScreen].bounds.size.height){
self.keyboardHeightConstraint.constant=300.0f;
[NSLayoutConstraint停用约束:self.landscapeConstraints];
[NSLayoutConstraint ACTIVATECONSTRAITS:self.tractiveConstraints];
}否则{
self.keyboardHeightConstraint.constant=400.0f;
[NSLayoutConstraint停用约束:自约束];
[NSLayoutConstraint激活约束:self.landscapeConstraints];
}
[super updateViewConstraints];
}

不确定,但这可能是autlayout的情况,我曾经迷恋过curdish键盘设计,所以我使用autolayout,它可能会有所帮助。我认为比较屏幕宽度和高度不是最好的方法。相反,请尝试访问状态栏方向,如下面的
[[UIApplication sharedApplication]statusBarOrientation]。或者您可以尝试获取设备的方向,如“[[UIDevice currentDevice]orientation]
。此外,在我看来,您的限制条件是“0x”按钮、空格按钮和返回按钮在纵向上具有固定宽度,而地球仪占据其余部分。然后,我猜你已经应用了纵横比,这将解释第一个“错误”键盘的外观。@SKT你建议我使用还是不使用自动布局?我不确定IB之外的默认值是多少。@mbm29414我使用宽度/高度比较,因为您无法在应用程序扩展中检查UIDevice方向,而statusBarOrientation有时会在设备因使用加速计而平放时返回未知响应。但我已经将NSLogs放在了我目前的方式中,它工作得很好——当它发生变化时,总是打印出正确的方向。
CGFloat _expandedHeight = 500;
NSLayoutConstraint *_heightConstraint = 
[NSLayoutConstraint constraintWithItem: self.view 
                             attribute: NSLayoutAttributeHeight 
                             relatedBy: NSLayoutRelationEqual 
                                toItem: nil 
                             attribute: NSLayoutAttributeNotAnAttribute 
                            multiplier: 0.0 
                              constant: _expandedHeight];
[self.view addConstraint: _heightConstraint];
- (void)updateViewConstraints {
    if (self.keyboardHeightConstraint == nil) {
        // Just starting with SOME value for the height
        self.keyboardHeightConstraint =
        [NSLayoutConstraint constraintWithItem:self.view
                                     attribute:NSLayoutAttributeHeight
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:nil
                                     attribute:NSLayoutAttributeNotAnAttribute
                                    multiplier:0.0f
                                      constant:500.0f];
        [self.view addConstraint:self.keyboardHeightConstraint];
    }
    // Obviously, these values will be changed based on device AND orientation
    // These are bogus values...
    if ([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height) {
        self.keyboardHeightConstraint.constant  = 300.0f;
        [NSLayoutConstraint deactivateConstraints:self.landscapeConstraints];
        [NSLayoutConstraint activateConstraints:self.portraitConstraints];
    } else {
        self.keyboardHeightConstraint.constant  = 400.0f;
        [NSLayoutConstraint deactivateConstraints:self.portraitConstraints];
        [NSLayoutConstraint activateConstraints:self.landscapeConstraints];
    }
    [super updateViewConstraints];
}