Ios 自定义键盘高度不小于';当设备方向改变时不会改变

Ios 自定义键盘高度不小于';当设备方向改变时不会改变,ios,iphone,custom-keyboard,ios8-extension,ios-keyboard-extension,Ios,Iphone,Custom Keyboard,Ios8 Extension,Ios Keyboard Extension,每当我旋转设备时,我的自定义键盘高度始终保持不变。我尝试删除约束并再次添加,但运气不佳。 我已经在iOS开发论坛上查看了很多关于堆栈溢出的相关问题,但我仍然没有找到解决方案 我读了一些用过的应用程序-(void)viewdide出现:(BOOL)动画,但一旦我使用此代码设置了大小: -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self updateCustomHeight];

每当我旋转设备时,我的自定义键盘高度始终保持不变。我尝试删除
约束
并再次添加,但运气不佳。 我已经在iOS开发论坛上查看了很多关于堆栈溢出的相关问题,但我仍然没有找到解决方案

我读了一些用过的应用程序
-(void)viewdide出现:(BOOL)动画
,但一旦我使用此代码设置了大小:

-(void)viewDidAppear:(BOOL)animated {

     [super viewDidAppear:animated];
    [self updateCustomHeight]; 
}


-(void)updateCustomHeight
{

    [self updateViewConstraints];
    CGFloat _expandedHeight = 250;

    NSLayoutConstraint *_heightConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:_expandedHeight];

    [self.view removeConstraint: _heightConstraint];
     [self.view layoutIfNeeded];

    if(isPortrait)
    {



        CGFloat _expandedHeight = 230;

        NSLayoutConstraint *_heightConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant: _expandedHeight];

        [self.view addConstraint: _heightConstraint];

    }
    else
    {

        CGFloat _expandedHeight = 200;

        NSLayoutConstraint *_heightConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant: _expandedHeight];

        [self.view addConstraint: _heightConstraint];
    }

}
但当我加载自定义键盘时,设置第一次设置框显示横向和纵向方向

肖像画

景观:

当我的方向更改委托时,我调用更改大小方法:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height){
        //Keyboard is in Portrait
        isPortrait=YES;

        [self LoadKeyboardview];
         [self updateCustomHeight];

    }
    else{

        isPortrait=NO;

        [self LoadKeyboardview];
         [self updateCustomHeight];

        //Keyboard is in Landscape
    }


}
-(void)将从接口方向:(UIInterfaceOrientation)旋转到接口方向持续时间:(NSTimeInterval)持续时间
{
如果([UIScreen mainScreen].bounds.size.width<[UIScreen mainScreen].bounds.size.height){
//键盘是纵向的
isPortrait=是;
[自动加载键盘视图];
[自我更新的重量];
}
否则{
isPortrait=否;
[自动加载键盘视图];
[自我更新的重量];
//键盘是横向的
}
}
注意:


对于键盘布局,我使用<强> Xb和加载NIB作为PAR设备定位,所有的工作都很好,但是在设备方向上的尺寸更新不能正常工作。在添加高度约束时,请帮助我将其保留在属性中,以便随后删除

@property (strong,nonatomic) NSLayoutConstraint *heightConstraint;

-(void)updateCustomHeight
{

    [self updateViewConstraints];
    CGFloat expandedHeight = 250;

    if (self.heightConstraint != nil) {
        [self.view removeConstraint:self.heightConstraint];
        [self.view layoutIfNeeded];

    if(isPortrait)
    {
       expandedHeight = 230;
    }
    else
    {

       expandedHeight = 200;
    }

    self.heightConstraint = [NSLayoutConstraint constraintWithItem:self.view      attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant: expandedHeight];
    [self.view addConstraint:self.heightConstraint];
    [self.view layoutIfNeeded];
}

尝试添加两个约束,一个用于纵向约束,另一个用于横向约束

- (void)updateViewConstraints {
    [super updateViewConstraints];

    if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height)
    {

        [self.view removeConstraint:_heightConstraintforLandscape];

    }
    else
    {
        [self.view removeConstraint:_heightConstraintforPortrait];
    }
}
-(void)updateViewConstraints{
[super updateViewConstraints];
如果([UIScreen mainScreen].bounds.size.width<[UIScreen mainScreen].bounds.size.height)
{
[self.view removeConstraint:_heightConstraintforLandscape];
}
其他的
{
[self.view removeConstraint:_heightConstraintforPortrait];
}
}
我能解决这个问题。这个问题是由于优先级问题。即使你设置了不同的优先级,它也会考虑初始值,我不知道为什么会这样。 无论如何,为风景和肖像设置不同的约束有助于我保持自定义高度。
希望它能帮助别人

由于您正在编写iOS 8扩展,因此应采用iOS 8方法。不要使用
willRotateToInterfaceOrientation:
您应该实现
视图WillTransitionToSize:
并实现大小类,而不是硬编码尺寸方向没有问题方向和加载nib工作文件问题是更改大小不起作用我已经实现了大小类。i不要认为删除高度约束的方法是正确的,因为您需要删除实际的约束对象实例,而不仅仅是等效约束-您正在删除视图上不存在的约束,因此它没有任何效果。您应该尝试创建与包含视图边缘相关的约束,而不是指定高度。主要的是,第一次默认的oriantation大小更改代码起作用,之后不起作用:(是的,因为您没有删除初始高度约束。您应该将其存储为属性,然后您可以删除实际的约束对象,而不是创建一个新的约束对象。谢谢您的回答,但我得到了相同的输出,没有任何更改@paulw11:(我认为我们在这里没有足够的内容进行讨论-例如,
updateViewConstraints
做什么?。您应该使用一个具有适当约束和大小类的情节提要-这样您就不需要在代码中执行任何操作。