【发布时间】:2018-08-21 09:44:49
【问题描述】:
我正在用 Swift 开发一个应用程序。 在我的故事板中,我有一个 UITableViewController。我想为我的表格应用左右边距以及第一个/最后一个单元格上的圆角以及表格视图周围的阴影。
我尝试了很多选项,但找不到合适的解决方案。现在,我正在使用 UITableViewCell 的子类,我在其中覆盖框架以使其更窄。我可以有圆角,但我不能有阴影。
这是我的子类代码:
class NarrowTableCell: UITableViewCell {
override var frame: CGRect {
get {
return super.frame
}
set (newFrame) {
var f = newFrame
f.origin.x = 10
if let w = superview?.frame.size.width {
f.size.width = w - 20
} else {
f.size.width = newFrame.size.width - 20
}
super.frame = f
super.layer.masksToBounds = true
}
}
func applyNoCorners() {
round(corners: [.topLeft, .topRight, .bottomLeft, .bottomRight], radius: 0)
}
func applyCorners() {
round(corners: [.topLeft, .topRight, .bottomLeft, .bottomRight], radius: 5)
}
func applyTopCorners() {
round(corners: [.topLeft, .topRight], radius: 5)
}
func applyBottomCorners() {
round(corners: [ .bottomLeft, .bottomRight], radius: 5)
}
}
我的大部分应用都是在 Storyboard 中完成的,我希望能够为动态和静态表格视图实现相同的结果。
我遇到的另一个问题是,当单元格框架更改时,我里面的标签没有正确更新。也就是说,它的内容被截断了。
这是我想做的:
【问题讨论】:
标签: ios swift uitableview shadow margins