iOS-AccessoryView更改自定义分隔符的大小
我有一个分组的tableview,起初我想删除顶部和底部分隔符。我环顾四周,找到了一个对我非常有效的解决方案。它是通过在单元格底部添加UIView来充当分隔符的。现在看起来很神奇。但我面临的问题是,当我设置单元格的AccessoryView时,因为某种原因,当我选择单元格时,我的自定义分隔符会更改其宽度。下面是问题的一个表现: 这是未选择的情况 选择此选项时: 请注意,自定义分隔符已具有约束 有什么建议吗 编辑: 下面是我如何设置accessoryView的:iOS-AccessoryView更改自定义分隔符的大小,ios,iphone,swift,xcode,Ios,Iphone,Swift,Xcode,我有一个分组的tableview,起初我想删除顶部和底部分隔符。我环顾四周,找到了一个对我非常有效的解决方案。它是通过在单元格底部添加UIView来充当分隔符的。现在看起来很神奇。但我面临的问题是,当我设置单元格的AccessoryView时,因为某种原因,当我选择单元格时,我的自定义分隔符会更改其宽度。下面是问题的一个表现: 这是未选择的情况 选择此选项时: 请注意,自定义分隔符已具有约束 有什么建议吗 编辑: 下面是我如何设置accessoryView的: override func t
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if let cell = tableView.cellForRow(at: indexPath) as? TableViewCell {
let label = UILabel()
label.text = "x"
label.sizeToFit()
label.font = UIFont.systemFont(ofSize: 13, weight: .medium)
if(cell.accessoryView == nil){
cell.accessoryView = label
}else{
cell.accessoryView = nil
}
}
}
就是这样,其他一切都只是一个带有行和节的简单表视图,我还在故事板中将分隔符设置为“无”
编辑2:
这是分隔符约束:
您需要更改尾部约束。将尾部约束添加到单元格,而不是ContentView。删除分隔符视图上的尾部约束并添加约束,如图所示。
在iOS 13+中,我必须更改代码,在单元格上绘制底部边框,而不是单元格的contentView。我在Cell类中的awakeFromNib中执行此操作
override func awakeFromNib() {
super.awakeFromNib()
addBorder(borderType: .bottom)
}
func addBorder(borderType: BorderType, width: CGFloat = 1.0, color: UIColor = .darkGray) {
// figure out frame and resizing based on border type
var autoresizingMask: UIView.AutoresizingMask
var layerFrame: CGRect
switch borderType {
case .left:
layerFrame = CGRect(x: 0, y: 0, width: width, height: self.bounds.height)
autoresizingMask = [ .flexibleHeight, .flexibleRightMargin ]
case .right:
layerFrame = CGRect(x: self.bounds.width - width, y: 0, width: width, height: self.bounds.height)
autoresizingMask = [ .flexibleHeight, .flexibleLeftMargin ]
case .top:
layerFrame = CGRect(x: 0, y: 0, width: self.bounds.width, height: width)
autoresizingMask = [ .flexibleWidth, .flexibleBottomMargin ]
case .bottom:
layerFrame = CGRect(x: 0, y: self.bounds.height - width, width: self.bounds.width, height: width)
autoresizingMask = [ .flexibleWidth, .flexibleTopMargin ]
}
// look for the existing border in subviews
var newView: UIView?
for eachSubview in self.subviews {
if eachSubview.tag == borderType.rawValue {
newView = eachSubview
break
}
}
// set properties on existing view, or create a new one
if newView == nil {
newView = UIView(frame: layerFrame)
newView?.tag = borderType.rawValue
self.addSubview(newView!)
} else {
newView?.frame = layerFrame
}
newView?.backgroundColor = color
newView?.autoresizingMask = autoresizingMask
}
你能为分隔符上的约束附加屏幕截图吗?@SaqibOmer我更新了我的question@SaqibOmer我还尝试添加一个宽度约束,但仍然存在相同的问题您可以通过使用简单的表视图和分段数据来实现这一点。@Aashish1aug您能详细说明一下吗?我是在cellForRowAtIndexPath方法中还是在didSelectRow中将分隔符放在前面?是的。更改您的委托方法,因为我添加了代码。我刚刚尝试了这两种方法,但都不起作用:(我只有SuperView和label作为约束中的第一项,我无法向单元格本身或contentView添加约束:在iOS 13中,还有谁会再次遇到此问题,即使有此修复?