我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
当前回答
这里有一个简单的方法全局删除嵌入。
在UITableViewCell +移到swift:
import UIKit
extension UITableViewCell {
override public var layoutMargins: UIEdgeInsets {
get { return UIEdgeInsetsZero }
set { }
}
}
在AppDelegate应用中:didFinishLaunchingWithOptions
UITableViewCell.appearance().separatorInset = UIEdgeInsetsZero
你可能会想a)也只是覆盖separatorInset在扩展,或b)设置外观代理的layoutmargin,而不是。这两种方法都行不通。即使separatorInset被指示为属性,尝试将其作为属性(或方法)重写也会产生编译器错误。并且为UITableViewCell的layoutmargin设置外观代理(或者,也为UITableView的layoutmargin和separatorInset设置外观代理)没有效果。
其他回答
我相信这是我在这里问的相同的问题:为XCode 6 iPhone模拟器删除iOS 8 UITableView上的SeparatorInset
在iOS 8中,所有继承自UIView的对象都有一个新属性。在ios7中设置SeparatorInset的解决方案。x将不能删除你在iOS 8中UITableView上看到的空白。
新的属性叫做“layoutmargin”。
@property(nonatomic) UIEdgeInsets layoutMargins
Description The default spacing to use when laying out content in the view.
Availability iOS (8.0 and later)
Declared In UIView.h
Reference UIView Class Reference
解决方案:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
如果你设置单元格。layoutmargin = UIEdgeInsetsZero;如果不检查layoutmargin是否存在,应用程序将在iOS 7.x上崩溃。所以,最好的方法是在setlayoutmargin:UIEdgeInsetsZero之前检查layoutmargin是否存在。
我是这样做的:
tableView.separatorInset = UIEdgeInsetsZero;
tableView.layoutMargins = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
使用Swift 2.2
创建UITableViewCell扩展
import UIKit
extension UITableViewCell {
func removeMargins() {
if self.respondsToSelector(Selector("setSeparatorInset:")) {
self.separatorInset = UIEdgeInsetsZero
}
if self.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
self.preservesSuperviewLayoutMargins = false
}
if self.respondsToSelector(Selector("setLayoutMargins:")) {
self.layoutMargins = UIEdgeInsetsZero
}
}
}
现在可以在cellForRowAtIndex中使用
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.removeMargins()//To remove seprator inset
}
Arg ! !在你的Cell子类中做这些事情后:
- (UIEdgeInsets)layoutMargins
{
return UIEdgeInsetsZero;
}
或者设置单元格。layoutmargin = UIEdgeInsetsZero;帮我修好了。
经过大量调查……
这是我能找到的完全控制这些东西的唯一方法
完全控制每个单元格上的分隔符插入和布局边距。在UITableviewDelegate的willDisplayCell方法中做到这一点。
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.layoutMargins = UIEdgeInsetsZero
cell.contentView.layoutMargins = UIEdgeInsetsMake(0, 10, 0, 10)
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
}
cell对象控制分隔符,contentView控制其他一切。如果你的分隔符插入空间显示在一个意想不到的颜色,这应该解决它:
cell.backgroundColor = cell.contentView.backgroundColor