iOS 9 UITableView分隔符插图(有效左边距)
我在iOS 9上的iOS 9 UITableView分隔符插图(有效左边距),ios,uitableview,uikit,xcode7,ios9,Ios,Uitableview,Uikit,Xcode7,Ios9,我在iOS 9上的UITableView中的UITableViewCells之间的分隔符有问题。它们有显著的左边缘。我已经有了删除iOS 8引入的间距的代码,但它不适用于iOS 9。看起来他们又加了些什么。我想这可能和你有关,但我还没弄明白。有没有人遇到过类似的问题并找到了解决方案?好的。唯一需要做的是在呈现实例UITableView上设置标志cellLayoutMarginsFollowReadableWidth - (void)tableView:(UITableView *)tableVi
UITableView
中的UITableViewCell
s之间的分隔符有问题。它们有显著的左边缘。我已经有了删除iOS 8引入的间距的代码,但它不适用于iOS 9。看起来他们又加了些什么。我想这可能和你有关,但我还没弄明白。有没有人遇到过类似的问题并找到了解决方案?好的。唯一需要做的是在呈现实例UITableView
上设置标志cellLayoutMarginsFollowReadableWidth
- (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];
}
}
myTableView.cellLayoutMarginsFollowReadableWidth = NO;
我想在文档中找到一些参考资料,但看起来还没有准备好
由于iOS 9中引入了向后兼容性标志,因此在尝试设置前应添加一个检查:
if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
{
myTableView.cellLayoutMarginsFollowReadableWidth = NO;
}
对于Swift 2.0
,您可以使用#available
检查iOS版本
if #available(iOS 9, *) {
myTableView.cellLayoutMarginsFollowReadableWidth = false
}
此外,您需要使用Xcode 7
或更高版本编译它
编辑
请记住,如果您的分隔符在iOS 8之前看起来“很好”,那么这是唯一需要的修复,否则您需要做更多的更改。您可以在SO上找到如何执行此操作的信息。这在iOS 9中非常适合我 对于OBJ-C
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(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];
}
return cell;
}
最高达iOS 9的完美解决方案 在viewDidLoad中 在TableViewDelegate方法中添加以下代码:
被接受的答案对我不起作用。直到我将
setCellLayoutMarginsFollowReadableWidth
移动到setLayoutMargins
之前(iOS 8仍然需要):
根据这里的不同答案,我能够用Swift中的以下代码行消除分隔符的间隙:
tableView.separatorInset = UIEdgeInsetsZero
tableView.layoutMargins = UIEdgeInsetsZero
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
但我仍然在课文之前有一个小小的差距:
适用于iOS 8和iOS 9
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([UITableView instancesRespondToSelector:@selector(setLayoutMargins:)]) [[UITableViewCell appearance] setLayoutMargins:UIEdgeInsetsZero];
if ([UITableView instancesRespondToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) [[UITableViewCell appearance] setPreservesSuperviewLayoutMargins:NO];
}
及
Swift 2.2 iOS 9.3
在viewDidLoad中
tableView.cellLayoutMarginsFollowReadableWidth = false
在UITableViewDelegates中
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector(Selector("setSeparatorInset:")){
cell.separatorInset = UIEdgeInsetsZero
}
if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
cell.preservesSuperviewLayoutMargins = false
}
if cell.respondsToSelector(Selector("setLayoutMargins:")){
cell.layoutMargins = UIEdgeInsetsZero
}
}
Swift 3.0/4.0
tableView.cellLayoutMarginsFollowReadableWidth = false
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
cell.separatorInset = UIEdgeInsets.zero
}
if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
cell.preservesSuperviewLayoutMargins = false
}
if cell.responds(to: #selector(setter: UIView.layoutMargins)) {
cell.layoutMargins = UIEdgeInsets.zero
}
}
如果您想在interface builder中执行此操作。默认的分隔符插入为
自动
。通过选择下拉列表将其更改为custom
这是我在XCode 8.2.1中针对Swift 3.0/iOS 10的解决方案 我已经为UITableview创建了一个子类,它可以用于IB并以编程方式创建TableView
import UIKit
class EXCSuperTV: UITableView
{
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
setupView()
}
override init(frame: CGRect, style: UITableViewStyle)
{
super.init(frame: frame, style: style)
setupView()
}
func setupView() {}
}
class EXCNoFooter: EXCSuperTV
{
override func setupView()
{
super.setupView()
//tableFooterView = UIView.Zero()
}
}
class EXCMainTV: EXCNoFooter
{
override func setupView()
{
super.setupView()
separatorInset = UIEdgeInsets.zero
}
}
我的情况不是这样。这在iOS第8版之前运行良好。苹果开发者论坛:这个答案可能会帮助其他人解决同样的问题:谢谢!是否有任何选项可以关闭整个应用程序的此功能,或者我必须手动更新所有TableViewController属性?您是否考虑过为tableview创建一个基类(在那里设置)并尽可能使用它?此外,我会看看它是否可以通过外观来设置。是的,这就是我现在正在做的(基类方法),我只是想知道是否会有某种“全局开关”来完全关闭它。我尝试了外观方面的东西,但这不适用于
[[UITableView外观]setCellLayoutMarginsFollowReadableWidth:NO]代码>这可能是下一个问题,因此如何(如果可能的话)全局设置它:)似乎对iOS 9.1没有任何影响。首先,如果你从另一个答案中复制代码,那么如果你提到原始作者并提供其答案的链接(这是公平的)。第二,您可以检查对象是否响应选择器,而不是检查iOS版本。@JulianKról您关于响应选择器的看法是正确的,这是一种替代方法。这是我的解决方案,我也为此创建了一个演示项目,可以在所有iOS版本上正常工作。如果你能欣赏的话,我会感觉很好。是的,但是我在SO上看到的代码的第二部分有相同的注释等。此外,将每件事都用粗体和斜体也不能提高其可读性:)此外,关于iOS 9的问题是明确的,假设在iOS 8之前,每件事都已经完成了(这是你答案的第二部分)。我感谢你的参与,但我会给你我的反馈。是的,这是我在iOS 8之前一直在使用的。但是对于iOS 9,除了此代码外,还必须设置self.testTableView.cellLayoutMarginsFollowReadableWidth=否。因此,为了得到正确的答案,我已经提到了完整的代码。那么,原始答案的链接呢?你没有说什么吗关于这一点:)作为一种解决方法,我刚刚为UITableView
设置了-14
约束,而不是使用上述代码手动对齐表格。这就是我所需要的!秩序很重要。对我来说,我注意到,在横向视图中,iPad桌面视图的顺序很重要。一旦旋转,它就被固定了,但是第一眼看到桌子是错误的。非常感谢。默认情况下,“分隔符插入”是自动的,在更改为自定义之前不显示左/右。
tableView.cellLayoutMarginsFollowReadableWidth = false
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector(Selector("setSeparatorInset:")){
cell.separatorInset = UIEdgeInsetsZero
}
if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
cell.preservesSuperviewLayoutMargins = false
}
if cell.respondsToSelector(Selector("setLayoutMargins:")){
cell.layoutMargins = UIEdgeInsetsZero
}
}
tableView.cellLayoutMarginsFollowReadableWidth = false
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
cell.separatorInset = UIEdgeInsets.zero
}
if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
cell.preservesSuperviewLayoutMargins = false
}
if cell.responds(to: #selector(setter: UIView.layoutMargins)) {
cell.layoutMargins = UIEdgeInsets.zero
}
}
import UIKit
class EXCSuperTV: UITableView
{
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
setupView()
}
override init(frame: CGRect, style: UITableViewStyle)
{
super.init(frame: frame, style: style)
setupView()
}
func setupView() {}
}
class EXCNoFooter: EXCSuperTV
{
override func setupView()
{
super.setupView()
//tableFooterView = UIView.Zero()
}
}
class EXCMainTV: EXCNoFooter
{
override func setupView()
{
super.setupView()
separatorInset = UIEdgeInsets.zero
}
}