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

我能解決這個問題。問題是由於優先。即使你設定了不同的優先級,它也會考慮最初的,我不知道爲什麼會這樣。

無論如何,設置不同的風景和肖像約束幫助我保持自定義的高度。 希望它可以幫助別人。