如何阻止 uitextview 覆盖 uitableviewcell? [英] How to stop a uitextview from covering uitableviewcell?

查看:17
本文介绍了如何阻止 uitextview 覆盖 uitableviewcell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在 tableview 中的 UITableViewCell 上有一个 UIView.我相信这个 UIView 将是这个表的 headerView.

So I have a UIView over a UITableViewCell in a tableview. I believe this UIView would be the headerView of this table.

在这个 headerView 中,我有一个 UITextview 并将其内部大小设置为占位符,以便它可以随着其内容增长.出于同样的原因,我还将 headerView 的固有大小设置为占位符.现在面临的问题是,当 UITextView 变大时,它开始隐藏 tableview 单元格.

inside this headerView I have a UITextview and am setting its intrinsic size to placeholder so It could grow with its content. I've also set the headerView's intrinsic size to placeholder for the same reason. now the problem am facing is that when the UITextView is getting big, it's starting to hide the tableview cells.

推荐答案

您需要计算文本视图的大小并将其返回到对内在内容大小的覆盖中,然后使用 systemLayoutSizeFittingSize: 来调整标题视图的大小.文本视图需要固定在单元格的顶部和底部(如果您在文本视图上方有视图,则可以将其固定在这些视图上,但这些视图需要固定在单元格的顶部).在我的测试应用程序中,我没有对 IB 中的 Intrinsic Size 设置进行任何操作,而是将其保留为系统定义.我子类化了文本视图并添加了这段代码,

You need to calculate the size of the text view and return that in an override of intrinsicContentSize, and then use systemLayoutSizeFittingSize: to resize the header view. The text view needs to be pinned to the top and bottom of the cell (if you have views above the text view, it can be pinned to those instead, but those views then need to be pinned to the top of the cell). In my test app, I didn't do anything with the Intrinsic Size setting in IB I left it as system defined. I subclassed the text view and added this code,

-(CGSize)intrinsicContentSize {
    CGRect textRect = [self.text boundingRectWithSize:CGSizeMake(self.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil];
    return CGSizeMake(textRect.size.width, textRect.size.height + self.textContainerInset.top + self.textContainerInset.bottom);
}

在表视图控制器中,我执行以下操作以在设置新文本时看到标题展开,

In the table view controller, I did the following to see the header expand when I set new text,

- (void)viewDidLoad {
    [super viewDidLoad];
    self.theData = @[@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine",@"Black",@"Brown",@"Red",@"Orange",@"Yellow",@"Green",@"Blue",@"Violet",@"Gray",@"White"];
    self.tableView.tableHeaderView = self.header;
    [self.tableView reloadData];
    [self performSelector:@selector(doStuff) withObject:nil afterDelay:3];
}


-(void)doStuff {
    self.tv.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. jfhk gdkj gkf fjkd jkgjhjh khj khkjhk kjh jkhjkh kjhkjhkjh  The quick red fox jumped over the lazy brown dog. The end";
    CGSize headerSize = [self.header systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    self.header.frame = CGRectMake(0, 0, headerSize.width, headerSize.height);
    self.tableView.tableHeaderView = self.header;
}

当我最初在 IB 中设置约束时,系统给了我一个警告,并提出了一些更改的建议——我只需要将垂直内容压缩阻力优先级更改为 749(从 750).我在这里上传了一个测试应用,http://jmp.sh/fLZv7XK

When I initially setup the constraints in IB, the system gave me a warning, and suggested several things to change -- I only needed to change the vertical content compression resistance priority to 749 (from 750). I uploaded a test app here, http://jmp.sh/fLZv7XK

这篇关于如何阻止 uitextview 覆盖 uitableviewcell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆