1、修改某一控件的某个约束宽,高等。例如:对高的修改
NSArray* constrains = self.segment.constraints;
for (NSLayoutConstraint* constraint in constrains) {
if (constraint.firstAttribute == NSLayoutAttributeHeight) {
constraint.constant = 0.0;
}
}
2、键盘弹出,遮挡输入框的修复
1)首先,添加监听(代码的位置视个人需求而定,如果界面内没有其他监听,最好在viewWillApper中添加,在disApper中移除)
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
2)添加辅助方法调整控件约束
- (void)replacePickerContainerViewTopConstraintWithConstant:(CGFloat)constant
{
NSArray* constrains = self.view.constraints;
for (NSLayoutConstraint* constraint in constrains) {
if (constraint.firstAttribute == NSLayoutAttributeHeight) {
constraint.constant = constant;
}
}
}
3)调整界面
//当键盘出现或改变时调用(调整view位置,适应键盘高度,即:让view在键盘上)
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGFloat height = keyboardRect.size.height;
float verson = [[[UIDevice currentDevice] systemVersion]floatValue];
if (verson >= 8.0){//大于8.0调约束
CGRect frame = self.nameField.frame;
CGRect bounds = self.view.bounds;
bounds.origin.y = height - (ScreenHeight - frame.origin.y - 50);
[self replacePickerContainerViewTopConstraintWithConstant:height];
[UIView animateWithDuration:0.25 animations:^{
if (bounds.origin.y > 0){
self.view.bounds = bounds;
[self.view layoutIfNeeded];
}
}];
}else{//小于8.0调frame
[UIView animateWithDuration:0.25 animations:^{
if (ScreenHeight < 500){
self.view.top = -100;
}
}];
}
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
float verson = [[[UIDevice currentDevice] systemVersion]floatValue];
if (verson >= 8.0){
CGRect bounds = self.view.bounds;
bounds.origin.y = 0;
[UIView animateWithDuration:0.25 animations:^{
self.view.bounds = bounds;
[self.view layoutIfNeeded];
}];
}else{//小于8.0调frame
[UIView animateWithDuration:0.25 animations:^{
if (ScreenHeight < 500){
self.view.top = 0;
}
}];
}
}