-
ios7的时候在storyboard 设置 TableView的separator intend = 0 可以让tableview的分割条顶到头。
但是,升级了iOS8时,发现不起作用了。
经过google,在 stackoverflow 发现了答案
翻译纪录一下
iOS8 在cell和tableview中都引入了layoutMargins属性,而且这个属性在iOS 7中并没有,所以你需要区别对待这两个版本。
使用 Ricky 的方案设置cell中的layoutMargin属性:
123456789-(
void
)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if
([cell respondsToSelector:
@selector
(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if
([cell respondsToSelector:
@selector
(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
12//按照作者最后的意思还要加上下面这一段 </p><p> if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){</p><p> [cell setPreservesSuperviewLayoutMargins:NO];</p><p> }</p>
}
你还需要在tableview中设置同样的属性. 通过多次试验,我发现你可以在viewDidLayoutSubviews.中设置这个属性
把下面代码拷贝到你的view的实现中
123456789101112-(
void
)viewDidLayoutSubviews
{
[
super
viewDidLayoutSubviews];
if
([self.tableView respondsToSelector:
@selector
(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if
([self.tableView respondsToSelector:
@selector
(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
}
这样,在ios7,ios8中都可以实现分割条顶头的需求了。(译者:ios6缺省是顶头的,因此这个方案可以适用于ios6-8).
Edit: 按照 @bffmike 在 Twitter 上所说, 你可能还需要在cell中设置 preservesSuperviewLayoutMargins=NO . 再次说明:每个人的情况因人而异.
ios8 UITableView设置 setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法
最新推荐文章于 2020-12-31 12:18:09 发布