欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS开发之uitextview动态适应高度讲解

程序员文章站 2022-07-05 23:36:41
ios开发之uitextview动态适应高度讲解 1. self.tableview.rowheight = uitableviewautomaticdimension;...

ios开发之uitextview动态适应高度讲解

1. 

self.tableview.rowheight = uitableviewautomaticdimension;

    self.tableview.estimatedrowheight = 44;

2. 在heightforrowatindexpath中设置return uitableviewautomaticdimension;

3. 

- (void)textviewdidchange:(uitextview *)textview {

    cgrect bounds = textview.bounds;

    //     计算 text view 的高度

    cgsize maxsize = cgsizemake(bounds.size.width, cgfloat_max);

    cgsize newsize = [textview sizethatfits:maxsize];

    bounds.size = newsize;

    textview.bounds = bounds;

    // 让 table view 重新计算高度

    //2.textview的高度不想等就更新 让 table view 重新计算高度

    if (bounds.size.height != self.contentview.bounds.size.height) {

        uitableview *tableview = [self tableview];

        [tableview beginupdates];

        [tableview endupdates];

    }

    self.contentview.bounds = bounds;

}

- (uitableview *)tableview {

    uiview *tableview = self.superview;

    while (![tableview iskindofclass:[uitableview class]] && tableview) {

        tableview = tableview.superview;

    }

    return (uitableview *)tableview;

}