我决定以编程方式创建我自己的 UITableView
分隔线,因为我需要精细控制在每个单独的 UITableViewCell
上方和/或下方显示分隔线。我的 tableView
有静态单元格,所以我没有在 cellForRowAtIndexPath
中创建分隔符。相反,我为每个单元格设置了 property
,在 viewDidLoad
中,我根据需要添加了顶部和/或底部分隔符。它一直在工作,直到我旋转到横向然后分隔线不会拉伸(stretch)以填满屏幕 - 它当然保持与创建时相同的宽度。我不知道如何自动调整它们以适合屏幕宽度。
我尝试添加自动布局约束(前导、尾随、顶部/底部),但由于某种原因它不起作用 - 宽度没有改变,但没有记录任何错误消息来指示约束有任何问题。分隔线有时也会在滚动或旋转时消失,如果我注释掉自动布局约束,它们就不会消失。
那么如何让我的自定义单元格分隔符在旋转时始终拉伸(stretch)以填充设备宽度,以及如何防止它们消失?
如果以不同的方式创建我的自定义单元格分隔符会更容易/更好,我愿意这样做。我只是不知道当单元格是静态时,除了我的方法之外如何做到这一点。我考虑过在 Storyboard 中创建 View ,并以可视化方式设置约束,但这不等同于我以编程方式所做的事情吗?如果它们是动态单元格,我会在 cellForRowAtIndexPath
中进行。
//In viewDidLoad:
[self addTopSeparatorForCell:self.myCell];
//Helper method
- (void)addTopSeparatorForCell:(UITableViewCell *)cell {
UIView *topSeparator = [[UIView alloc] initWithFrame:CGRectMake(15, 1, cell.contentView.frame.size.width, 0.5)];
//add CALayer to preserve line separator visibility when row is highlighted
CALayer *backgroundColorLayer = [CALayer layer];
backgroundColorLayer.frame = topSeparator.bounds;
backgroundColorLayer.backgroundColor = [UIColor colorWithWhite:204/255.0f alpha:1].CGColor;
[topSeparator.layer addSublayer:backgroundColorLayer];
[cell.contentView addSubview:topSeparator];
//add auto layout constraints
topSeparator.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *cn = nil;
cn = [NSLayoutConstraint constraintWithItem:topSeparator
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:cell.contentView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:15];
[cell.contentView addConstraint:cn];
cn = [NSLayoutConstraint constraintWithItem:topSeparator
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:cell.contentView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0];
[cell.contentView addConstraint:cn];
cn = [NSLayoutConstraint constraintWithItem:topSeparator
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:cell.contentView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:1];
[cell.contentView addConstraint:cn];
}
编辑:感谢@ user1966109,我们已经能够解决线条未延伸以填充宽度的问题,现在在突出显示单元格时它们会被保留。但是仍然有一个问题我无法解决,因为我不确定它为什么会发生。向下滚动并向上滚动后分隔线消失。它与自动布局约束有关,因为以前有其他问题的解决方案没有出现此问题。这是导致线条消失的当前解决方案。如果有人知道如何防止此问题(并保留已解决的其他问题),我将不胜感激。
[self.cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(15@750)-[myView]-(-47@750)-|" options:0 metrics:0 views:viewsDictionary]];
[self.cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[myView(2)]-(-2@750)-|" options:0 metrics:0 views:viewsDictionary]];
最佳答案
您不应混合使用 initWithFrame 和自动布局。使用可视格式语言进行自动布局,只需几行代码即可获得良好的结果:
//@interface TableViewController ()
@property (weak, nonatomic) IBOutlet UITableViewCell *cell;
//@implementation TableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *myView = [[UIView alloc] init];
myView.backgroundColor = [UIColor redColor];
[self.cell.contentView addSubview:myView];
myView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(myView);
[self.cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[myView]|" options:0 metrics:0 views:viewsDictionary]];
[self.cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[myView(2)]" options:0 metrics:0 views:viewsDictionary]];
}
这可以完美地处理旋转。
编辑!
如果使用辅助 View ,请设置以下约束:
//Set a negative value to the trailing space in order to display myView under the accessory view
//Those constraints work for both self.cell and self.cell.contentView (kind of odd)
[self.cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(15@750)-[myView]-(-47@750)-|" options:0 metrics:0 views:viewsDictionary]];
[self.cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[myView(2)]-(-2@750)-|" options:0 metrics:0 views:viewsDictionary]];
关于ios - 如何在横向上调整自定义 UITableView 分隔符的大小并防止消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23818278/