iOS 9 UITableView 分隔符插入(显着的左边距)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31537196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS 9 UITableView separators insets (significant left margin)
提问by Julian Król
I have a problem with separators between UITableViewCell
s in UITableView
on iOS 9
. They have the significant left margin. I already have code for removing spacing introduced by iOS 8
but it doesn't work with iOS 9
. It looks like they added something else. I suppose it might be connected with layoutMarginsGuidebut I haven't figured it out yet. Does anyone had a similar problem and found out the solution?
我对UITableViewCell
s in UITableView
on之间的分隔符有问题iOS 9
。他们有显着的左边距。我已经有删除由 引入的间距的代码,iOS 8
但它不适用于iOS 9
. 看起来他们添加了其他东西。我想它可能与layoutMarginsGuide 相关,但我还没有弄清楚。有没有人遇到过类似的问题并找到了解决方案?
回答by Julian Król
Okay, I have found out the solution. The only thing required for that is to set on the presenting instance of UITableView
that flag cellLayoutMarginsFollowReadableWidth
好的,我已经找到了解决方案。唯一需要的是在该UITableView
标志的呈现实例上进行设置cellLayoutMarginsFollowReadableWidth
myTableView.cellLayoutMarginsFollowReadableWidth = NO;
I wanted to find some reference in the documentation but it looks like it is not ready yet, only mentioned on diff page.
我想在文档中找到一些参考,但看起来还没有准备好,只在 diff page 上提到。
As the flag was introduced in iOS 9 for the backward compatibility you should add a check before trying to set it:
由于该标志是在 iOS 9 中为了向后兼容性而引入的,因此您应该在尝试设置它之前添加一个检查:
if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
{
myTableView.cellLayoutMarginsFollowReadableWidth = NO;
}
For Swift 2.0
you can use #available
to check iOS version.
因为Swift 2.0
你可以#available
用来检查 iOS 版本。
if #available(iOS 9, *) {
myTableView.cellLayoutMarginsFollowReadableWidth = false
}
Moreover you need to compile it with Xcode 7
or above.
此外,您需要使用Xcode 7
或以上编译它。
EDIT
编辑
Please keep in mind that this is the only required fix if your separators looked "fine" up to iOS 8, otherwise you need to change a bit more. You can find info how to do this already on SO.
请记住,如果您的分隔符在 iOS 8 之前看起来“很好”,这是唯一需要的修复,否则您需要进行更多更改。您可以在 SO 上找到如何执行此操作的信息。
回答by ArunGJ
回答by Huy Vu
Swift 2.2 iOS 9.3
斯威夫特 2.2 iOS 9.3
In viewDidLoad
在视图中加载
tableView.cellLayoutMarginsFollowReadableWidth = false
In UITableViewDelegates
在 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
}
}
回答by derdida
Swift 3.0 / 4.0
斯威夫特 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
}
}
回答by Bhuvan Bhatt
Perfect Solution upto iOS 9
iOS 9 的完美解决方案
In viewDidLoad
在视图中加载
- (void)viewDidLoad {
[super viewDidLoad];
//Required for iOS 9
if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 9.0) {
self.testTableView.cellLayoutMarginsFollowReadableWidth = NO;
}
}
In TableViewDelegate methods add following code:
在 TableViewDelegate 方法中添加以下代码:
- (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];
}
}
回答by codelearner
Based on different answers here, I am able to remove the gap from separator with these lines of codes in Swift:
根据此处的不同答案,我可以使用 Swift 中的这些代码行消除分隔符中的间隙:
tableView.separatorInset = UIEdgeInsetsZero
tableView.layoutMargins = UIEdgeInsetsZero
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
But still I am having this small gap before the text:
但是我在文本之前仍然有这个小差距:
回答by jithin
This worked perfectly for me in iOS 9.
这在 iOS 9 中非常适合我。
For OBJ-C
对于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;
}
回答by Donnit
The accepted answer did not work for me. Until I moved setCellLayoutMarginsFollowReadableWidth
BEFORE setLayoutMargins
(still needed for iOS 8):
接受的答案对我不起作用。直到我搬家setCellLayoutMarginsFollowReadableWidth
之前setLayoutMargins
(iOS 8 仍然需要):
if([_tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) {
_tableView.cellLayoutMarginsFollowReadableWidth = NO;
}
if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
_tableView.layoutMargins = UIEdgeInsetsZero;
}
回答by Javier Sánchez
For iOS 8 and 9
对于 iOS 8 和 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];
}
and
和
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) [cell setLayoutMargins:UIEdgeInsetsZero];
}
回答by Mihawk
- (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];
}
}