iOS 8.0 在单元格和表格视图上引入了layoutMargins 属性。
此属性在 iOS 7.0 上不可用,因此您需要确保在分配它之前进行检查!
简单的解决方法是按照@user3570727的建议对单元格进行子类化并覆盖布局边距属性。但是,您将失去任何系统行为,例如从安全区域继承边距,因此我不推荐以下解决方案:
(目标C)
-(UIEdgeInsets)layoutMargins {
return UIEdgeInsetsZero // override any margins inc. safe area
}
(迅速4.2):
override var layoutMargins: UIEdgeInsets { get { return .zero } set { } }
如果您不想覆盖该属性,或者需要有条件地设置它,请继续阅读。
除了layoutMargins
属性,苹果添加了一个property到您的单元格,这将阻止它继承您的表格视图的边距设置。设置此属性后,单元格可以独立于表格视图配置自己的边距。将其视为覆盖。
这个属性被称为preservesSuperviewLayoutMargins
,并将其设置为NO
将允许细胞的layoutMargin
设置覆盖任何内容layoutMargin
在您的 TableView 上设置。既节省时间(您不必修改表视图的设置),并且更加简洁。详细解释请参考 Mike Abdullah 的回答。
注意:下面是一个干净的实现单元格级边距设置,正如迈克·阿卜杜拉的回答中所表达的那样。设置您的手机preservesSuperviewLayoutMargins=NO
将确保您的表视图不会覆盖单元格设置。如果您确实希望整个表格视图具有一致的边距,请相应地调整您的代码。
设置单元格边距:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
// Prevent the cell from inheriting the Table View's margin settings
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
// Explictly set your cell's layout margins
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
Swift 4:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// Remove seperator inset
if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
cell.separatorInset = .zero
}
// Prevent the cell from inheriting the Table View's margin settings
if cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {
cell.preservesSuperviewLayoutMargins = false
}
// Explictly set your cell's layout margins
if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
cell.layoutMargins = .zero
}
}
设置preservesSuperviewLayoutMargins
您手机上的属性为“否”should防止表格视图覆盖单元格边距。在某些情况下,它似乎无法正常运行。
如果全部失败,您可以暴力破解您的表视图边距:
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
// Force your tableview margins (this may be a bad idea)
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
}
Swift 4:
func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Force your tableview margins (this may be a bad idea)
if tableView.responds(to: #selector(setter: UITableView.separatorInset)) {
tableView.separatorInset = .zero
}
if tableView.responds(to: #selector(setter: UITableView.layoutMargins)) {
tableView.layoutMargins = .zero
}
}
...就这样吧!这应该适用于 iOS 7 和 8。
EDIT: Mohamed Saleh 提醒我注意 iOS 9 中可能发生的变化。您可能需要设置表视图的cellLayoutMarginsFollowReadableWidth
to NO
如果您想自定义插图或边距。您的里程可能会有所不同,这没有很好的记录。
该属性仅存在于 iOS 9 中,因此请务必在设置前检查。
if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
{
myTableView.cellLayoutMarginsFollowReadableWidth = NO;
}
Swift 4:
if myTableView.responds(to: #selector(setter: self.cellLayoutMarginsFollowReadableWidth)) {
myTableView.cellLayoutMarginsFollowReadableWidth = false
}
(以上代码来自iOS 8 UITableView 分隔符插入 0 不起作用 https://stackoverflow.com/questions/25770119/ios-8-uitableview-separator-inset-0-not-working/32860532#32860532)
编辑:这是一个纯粹的界面生成器方法:
注意:iOS 11 改变并简化了大部分行为,更新即将发布......