How to stop uitextview from covering uitableviewcell?

So, I have a UIView over a UITableViewCell as a table. I believe that this UIView will be the headerView of this table.

inside this headerView I have a UITextview and I set my own size to the placeholder so that it can grow with its contents. For the same reason, I also set the internal size of the ViewView header. now the problem is that when a UITextView gets large, it starts hiding tableview cells.

enter image description here

0
source share
1 answer

You need to calculate the size of the text view and return it in an override of intrinsicContentSize, and then use systemLayoutSizeFittingSize: resize the header. The text view should be attached to the top and bottom of the cell (if you have views above the text view, you can attach them to them instead, but these views should then be docked at the top of the cell). In my test application, I did nothing with setting the Intrinsic Size in IB, I left it as a specific system. 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 how the header expands 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; } 

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

0
source

Source: https://habr.com/ru/post/1207532/


All Articles