2015-02-05 67 views
1

所以,很像Messages應用程序,我有一個佔用大部分屏幕的UITableView。在UITableView下面,我有一個包含UITextView和UIButton的UIView。當用戶點擊UITextView時,鍵盤出現。我已經成功地將UIView移動到了鍵盤的頂部(這就是我的bottomConstraint插座所做的),但我也在努力移動UITableView。當鍵盤出現時,無法更改UITableView高度約束使用Autolayout

現在,我對UITableView設置爲528的高度約束。我已將該約束連接到Outlet,這裏是我的keyboardWillShow方法。

- (void)keyboardWillShow:(NSNotification *)sender { 

    NSDictionary *info = [sender userInfo]; 
    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    CGRect frame = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    CGRect newFrame = [self.view convertRect:frame fromView:[[UIApplication sharedApplication] delegate].window]; 
    self.bottomConstraint.constant = newFrame.origin.y - CGRectGetHeight(self.view.frame); 

    CGFloat height = CGRectGetHeight(self.view.frame) - self.composeTextView.frame.size.height - newFrame.origin.y; 
    self.tableViewHeightConstraint.constant = height; 

    [UIView animateWithDuration:animationDuration animations:^{ 
     [self.view layoutIfNeeded]; 
    }];} 

下面是我的約束/故事板的截圖。

Storyboard

^^視圖控制器

UITableView Constraints

^^ UITableView的約束

UIView Constraints

^^ UIView的約束(含有鍵+ TextView的視圖)

這是我的錯誤:

無法同時滿足約束條件。 以下列表中的至少一個約束可能是您不想要的。試試這個:(1)看看每個約束,並試圖找出你不期望的; (2)找到添加不需要的約束或約束並修復它的代碼。 (注意:如果你看到,你不明白NSAutoresizingMaskLayoutConstraints,請參閱文檔UIView的財產translatesAutoresizingMaskIntoConstraints) (

"<NSLayoutConstraint:0x174280320 V:[UITableView:0x135031c00(528)]>", 

"<NSLayoutConstraint:0x17409ff40 V:[UIView:0x174184ac0(40)]>", 

"<NSLayoutConstraint:0x1742807d0 V:|-(0)-[UITableView:0x135031c00] (Names: '|':UIView:0x174184b90)>", 

"<NSLayoutConstraint:0x1742808c0 V:[UITableView:0x135031c00]-(0)-[UIView:0x174184ac0]>", 

"<_UILayoutSupportConstraint:0x1740b9d40 V:[_UILayoutGuide:0x1741b3b00(0)]>", 

"<_UILayoutSupportConstraint:0x1740b9ce0 _UILayoutGuide:0x1741b3b00.bottom == UIView:0x174184b90.bottom>", 

"<NSLayoutConstraint:0x174280690 UIView:0x174184ac0.bottom == _UILayoutGuide:0x1741b3b00.top - 224>", 

"<NSLayoutConstraint:0x174280dc0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x174184b90(568)]>" 

將嘗試打破約束

恢復

NSLayoutConstraint:0x174280320 V:[UITableView:0x135031c00(528)]

回答

2

像這樣的東西應該工作,因爲你只需要設置高度不需要計算新的框架S:

- (void)keyboardWillShow:(NSNotification *)sender { 

NSDictionary *info = [sender userInfo]; 
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
CGRect keyboardFrame = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue]; 


CGFloat height = self.tableViewHeightConstraint.constant-keyboardFrame.size.height-self.composeTextView.frame.size.height; 
self.tableViewHeightConstraint.constant = height; 

[UIView animateWithDuration:animationDuration animations:^{ 
    [self.view layoutIfNeeded]; 
}];} 

而且看來,如果你只是設置的tableView的底部約束是composeTextView的頂部,而不是使用一個固定的高度像會更容易些。那會一直工作。

+0

將UITableView的底部約束設置爲包含UITextView + UIButton的視圖的頂部工作完美。 – 2015-02-05 15:11:15

相關問題