美文网首页
iOS编程,可视化布局,代码修改控件优先级

iOS编程,可视化布局,代码修改控件优先级

作者: 霍伟健 | 来源:发表于2016-04-01 11:53 被阅读408次

以textField为例,点击textField 弹出键盘时改变所有的frame。


// 添加键盘将要显示的通知.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    // 添加将要隐藏的通知.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden:) name:UIKeyboardWillHideNotification object:nil];

#pragma mark ** 键盘将要显示
- (void)keyboardWillShow:(NSNotification *)noti {
    // 监听方法会执行多次, 通过BOOL值筛选掉重复的执行.
    if (!_isBegin) {
        
        // 修改约束的优先级为 749.
        [self textFieldConstraint:self.layoutConstraintForTextField priority:749 fromView:self.view];
        [self textFieldConstraint:self.layoutConstraintForButton priority:749 fromView:self.view];
        _isBegin ^= 1;
        
    }
}

#pragma mark ** 键盘将要隐藏
- (void)keyboardWillHiden:(NSNotification *)noti {
    // 如果isBegin为Yes.
    // 说明键盘已经弹出.
    if (_isBegin) {
        
        // 修改约束的优先级 900.
        [self textFieldConstraint:self.layoutConstraintForTextField priority:900 fromView:self.view];
        [self textFieldConstraint:self.layoutConstraintForButton priority:900 fromView:self.view];
        _isBegin ^= 1;
    }
}

#pragma mark ** 修改优先级方法
- (void)textFieldConstraint:(NSLayoutConstraint *)constraint priority:(float)priority fromView:(UIView *)view {
    
    constraint.priority = priority;
    [UIView animateWithDuration:1.5f animations:^{
         // 重新布局.
        [view layoutIfNeeded];
    }];
    
}

相关文章

网友评论

      本文标题:iOS编程,可视化布局,代码修改控件优先级

      本文链接:https://www.haomeiwen.com/subject/htlylttx.html