tableView中所有行的高度都是固定的71pt。
![enter image description here](https://istack.dev59.com/D7EjF.webp)
tableView.estimatedRowHeight
以消除空间。@urgentx的答案基本满足我的需求,但并没有完全解决问题。我也遇到了类似的情况(聊天应用中的倒置UITableView),但是他们的建议完全禁用了安全区域行为,这对于像iPhone X这样的设备并不理想,因为这意味着tableview将被下方的home指示器和任何顶部导航栏裁剪。
为了使tableview在倒置时仍能正确避开安全区域,我进行了以下操作:
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
self.tableView.contentInsetAdjustmentBehavior = .never
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
//fetch the safe area in layoutSubviews since this it's possible that they can change when the device is rotated
let safeArea = self.tableView.safeAreaInsets
//insets are **flipped** because the tableview is flipped over its Y-axis!
self.tableView.contentInset = .init(top: safeArea.bottom, left: 0, bottom: safeArea.top, right: 0)
}
contentInsetAdjustmentBehavior = .automatic
的行为,但将插图沿Y轴翻转。我曾经遇到过相同的问题,在最新版本的Xcode (11.3) 和 iOS (13.3) iPhone上。我尝试过很多解决方法,但都没有奏效。
我的 tableView
有上、下、左、右约束,但是在最后一个单元格下方有很大的空间。(tableView.contentSize.height
几乎是所需高度的两倍)。tableView
的数据并不是动态的。这种情况发生在显示它的视图控制器显示和 tableView
再次隐藏时再次显示时都会发生。
我通过使用scrollView的代理方法来解决这个问题,当用户开始拖动时,高度自动调整。对我而言,这个方法非常有效,希望对你也有用!
// this is the best place to adjust the contentSize.height of tableView.
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("scrollViewWillBeginDragging():") // optional
// replace allParkingGeos.count with your data for tableView
tableView.contentSize.height = CGFloat(allParkingGeos.count) * cellHeight
}
tableView.tableFooterView = UIView();
或者您可以像下面这样做,- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger )section {
if(section == 0) {
return 6;
} else {
return 1.0;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger )section {
return 5.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger )section {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger )section {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}
当我将UITableView翻转用于聊天消息屏幕时,遇到了这个问题。
在翻转TableView之前,内容插入会留下一个空白空间在顶部,这样当我们滚动到最上方时,内容不会被导航栏遮挡。一旦翻转,插入就移动到底部。
在您的UITableView的Size Inspector中将“Content Insets”更改为Never,以消除末尾的空白空间。