标签:
例如: 跟随键盘弹出的ToolBar,原来在视图底部,当键盘弹出时,ToolBar跟随键盘弹出
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *toolViewBottomConstraint;
//键盘的通知(显示)
- (void)keyboardWillShow:(NSNotification *)notification
{
NSValue* aValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
NSNumber *durationValue = [notification userInfo][UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration = durationValue.doubleValue;
[UIView animateWithDuration:animationDuration delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_toolViewBottomConstraint.constant = keyboardRect.size.height;//修改距离底部的约束
} completion:^(BOOL finished) {
}];
[self.view setNeedsLayout]; //更新视图
[self.view layoutIfNeeded];
}
下面来看下,如何删除和增加约束 最后,附个addConstraint 函数的意义: view1.attr1 = view2.attr2 * multiplier + constant
[self.view removeConstraint:_sinaLeftDistance];//在父试图上将iSinaButton距离屏幕左边的约束删除
NSLayoutConstraint *myConstraint =[NSLayoutConstraint
constraintWithItem:iSinaButton //子试图
attribute:NSLayoutAttributeCenterX //子试图的约束属性
relatedBy:NSLayoutRelationEqual //属性间的关系
toItem:self.view//相对于父试图
attribute:NSLayoutAttributeCenterX//父试图的约束属性
multiplier:1.0
constant:0.0];// 固定距离
[self.view addConstraint: myConstraint];//为iSinaButton重新添加一个约束
标签:
原文地址:http://www.cnblogs.com/gaohe/p/5799605.html