Jul*_*ról 49 uitableview uikit ios ios9 xcode7
我有之间的分隔符的问题UITableViewCell以s UITableView上iOS 9.它们具有显着的左边距.我已经有了删除引入的间距的代码,iOS 8但它无法使用iOS 9.看起来他们添加了别的东西.我想它可能与layoutMarginsGuide有关,但我还没想到它.有没有人有类似的问题,并找到了解决方案?
Jul*_*ról 69
好的,我找到了解决方案.唯一需要的是设置UITableView该标志的呈现实例cellLayoutMarginsFollowReadableWidth
myTableView.cellLayoutMarginsFollowReadableWidth = NO;
Run Code Online (Sandbox Code Playgroud)
我想在文档中找到一些参考,但它看起来还没有准备好,只在diff页面上提到过.
由于该标志是在iOS 9中为了向后兼容性而引入的,因此您应该在尝试设置它之前添加一个检查:
if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
{
myTableView.cellLayoutMarginsFollowReadableWidth = NO;
}
Run Code Online (Sandbox Code Playgroud)
对于Swift 2.0您可以使用#available检查的iOS版本.
if #available(iOS 9, *) {
myTableView.cellLayoutMarginsFollowReadableWidth = false
}
Run Code Online (Sandbox Code Playgroud)
此外,您需要使用Xcode 7或以上编译它.
编辑
请记住,如果您的分隔符在iOS 8中看起来"很好",这是唯一需要的修复,否则您需要更改一点.您可以在SO上找到有关如何执行此操作的信息.
小智 18
Swift 2.2 iOS 9.3
在viewDidLoad中
tableView.cellLayoutMarginsFollowReadableWidth = false
Run Code Online (Sandbox Code Playgroud)
在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
}
}
Run Code Online (Sandbox Code Playgroud)
Bhu*_*att 11
适用于iOS 9的完美解决方案
在viewDidLoad中
- (void)viewDidLoad {
[super viewDidLoad];
//Required for iOS 9
if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 9.0) {
self.testTableView.cellLayoutMarginsFollowReadableWidth = NO;
}
}
Run Code Online (Sandbox Code Playgroud)
在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];
}
}
Run Code Online (Sandbox Code Playgroud)
der*_*ida 11
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
}
}
Run Code Online (Sandbox Code Playgroud)
这在 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;
}
Run Code Online (Sandbox Code Playgroud)
基于这里的不同答案,我能够在Swift中使用这些代码行消除与分隔符之间的差距:
tableView.separatorInset = UIEdgeInsetsZero
tableView.layoutMargins = UIEdgeInsetsZero
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
Run Code Online (Sandbox Code Playgroud)
但我仍然在文本之前有这个小差距:
| 归档时间: |
|
| 查看次数: |
34046 次 |
| 最近记录: |