所以,很像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];
}];}
下面是我的約束/故事板的截圖。
^^視圖控制器
^^ UITableView的約束
^^ 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)]
將UITableView的底部約束設置爲包含UITextView + UIButton的視圖的頂部工作完美。 – 2015-02-05 15:11:15