美文网首页
iOS Masonry布局cell 自适应高度问题(mas_up

iOS Masonry布局cell 自适应高度问题(mas_up

作者: 橙_知足常乐 | 来源:发表于2021-10-15 10:06 被阅读0次

注:(记下以便往后查看)
问题:根据键盘更改UITextField的height高度,mas_updateConstraints 无效,cell高度无法自适应不改变。

解决:回调到cell上刷新tableview

  __weak typeof(object) weakSelf = self;
 [cell setInputAction:^{
          // 刷新tableview,更改高度,这个方法不会让页面跳动
           [weakSelf.mTableView beginUpdates];
           [weakSelf.mTableView endUpdates];
  }];

cell的代码例子

//
//  DynamicInputCell.m
//  ShuGouYiXuan
//
//  Created by 点购科技 on 2021/10/14.
//

#import "DynamicInputCell.h"

@implementation DynamicInputCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
 
        [self setupUI];
        
        //键盘显示 回收的监听
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

-(void)setupUI{
    
    [self addSubview:self.downImg];
    [self.downImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.right.equalTo(self).offset(-50);
        make.width.equalTo(@(20));
        make.height.equalTo(@(10));
    }];
    
    [self insertSubview:self.bgView aboveSubview:self.contentView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.downImg.mas_bottom);
        make.left.equalTo(self).offset(65);
        make.right.equalTo(self).offset(-30);
        make.bottom.equalTo(self);
    }];
    
    [self.bgView addSubview:self.inputTF];
    [self.inputTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).offset(15);
        make.left.equalTo(self.bgView).offset(15);
        make.right.equalTo(self.bgView).offset(-15);
        make.bottom.equalTo(self.bgView).offset(-15);
        make.height.equalTo(@(32));
    }];
    
}


#pragma mark --- 键盘显示监听事件 --
- (void)keyboardWillChange:(NSNotification *)noti{
    
    if(noti.name == UIKeyboardWillHideNotification){//隐藏
        [self.inputTF mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(32));
        }];

    }else{//显示
        [self.inputTF mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(0));
        }];

    }
    //回调通知tableView更新
    if (self.InputAction) {
        self.InputAction();
    }
}

//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//
//    return NO;
//}


#pragma mark - 懒加载
-(UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = BGColor;
        [UseTool setLayerToView:_bgView reduceW:95 type:0];
    }
    return _bgView;
}


- (UITextField *)inputTF{
    if (!_inputTF) {
        _inputTF = [[UITextField alloc]init];
        _inputTF.placeholder = @"评论一下";
        _inputTF.layer.cornerRadius = 3;
        _inputTF.layer.masksToBounds = YES;
        _inputTF.backgroundColor = WhiteColor;
        _inputTF.font = [UIFont systemFontOfSize:12];
        _inputTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 30)];
        _inputTF.leftViewMode = UITextFieldViewModeAlways;
        _inputTF.delegate = self;
    }
    
    return _inputTF;
}

//三角形
-(TriangleView *)downImg{
    if (!_downImg) {
        _downImg = [[TriangleView alloc] initWithColor:BGColor style:triangleViewIsoscelesTop];
    }
    return _downImg;
}
@end

相关文章

网友评论

      本文标题:iOS Masonry布局cell 自适应高度问题(mas_up

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