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

以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];
    }];
    
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容