0

每当我旋转设备时,我的自定义键盘高度始终保持不变。我尝试删除Constraints并再次添加,但没有运气。 我已经在很多相关的问题对堆栈溢出,并在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]; 
    } 

} 

但是,当我加载自定义键盘设置第一次设置帧显示与横向以及纵向。

肖像

enter image description here

景观:

enter image description here

我打电话说改变大小方法时,我的方向变化代表:

-(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和负载笔尖与参数设备方向的键盘布局,所有工作正常,但在设备方向尺寸的该更新是不会工作的。请在此帮助我

+0

由于您正在编写iOS 8扩展,您应该采用iOS 8方法。而不是使用'willRotateToInterfaceOrientation:'你应该实现'viewWillTransitionToSize:'并且实现尺寸类而不是硬编码尺寸 – Paulw11 2014-10-09 09:52:11

+0

那么定向和加载笔尖工作文件问题的方向没有问题,那就是我已经在实施改变尺寸了大小班。 – 2014-10-09 09:53:16

+0

我不认为你的方法去除高度限制是正确的,因为你需要删除实际的约束对象实例,而不仅仅是一个等价的约束 - 你正在删除一个约束,它不存在于视图中,所以它没有影响。您应该尝试创建相对于包含视图边缘的约束,而不是指定高度 – Paulw11 2014-10-09 09:58:10

回答

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]; 
} 
+0

谢谢你的回答,但我得到相同的输出没有什么变化@ paulw11 :( – 2014-10-09 10:19:35

+0

我不认为我们有足够的力量去这里 - 例如,'updateViewConstraints'做什么?你应该真的使用一个具有适当约束的故事板和大小类 - 那么你不需要在代码中做任何事情。 – Paulw11 2014-10-09 10:20:57

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]; 
    } 
} 

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

无论如何,设置不同的风景和肖像约束帮助我保持自定义的高度。 希望它可以帮助别人。