2

每当我旋转设备时,我的自定义键盘高度始终保持不变。我尝试删除Constraints并再次添加它,但没有运气。我在 Stack Overflow 和 iOS 开发论坛上查看了很多相关问题,但我仍然没有解决方案。

我阅读了一些使用过的应用程序-(void)viewDidAppear:(BOOL)animated,但是一旦我使用此代码设置了大小:

-(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
    }


}

笔记:

对于键盘布局,我使用XIB并将 nib 加载为 par 设备方向,一切正常,但设备方向的大小更新不起作用。请帮助我

4

2 回答 2

1

添加高度约束时,您需要将其保留在属性中,以便随后将其删除。

@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];
}
于 2014-10-09T10:08:59.330 回答
0

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

- (void)updateViewConstraints {
    [super updateViewConstraints];

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

        [self.view removeConstraint:_heightConstraintforLandscape];

    }
    else
    {
        [self.view removeConstraint:_heightConstraintforPortrait];
    }
}

我能够解决这个问题。问题在于优先级。即使您设置了不同的优先级,它也会考虑最初的优先级,我不知道为什么会这样。

无论如何,为横向和纵向设置不同的约束有助于我保持自定义高度。希望它可以帮助某人。

于 2014-12-31T09:54:03.940 回答