【发布时间】:2015-08-01 15:43:23
【问题描述】:
我有一个 tableview 带有自定义单元格,这些单元格是从 storyboard 构建的,标识符使用 AutoLayout。
其中一个subviews需要是圆形的(layer.cornerRadius = width/2),开头是正方形。
我在layoutSubviews() 中尝试过,但它似乎在AutoLayout 更改其大小之前被调用...didMoveToSuperview() 也是如此
AutoLayout 更改大小后,将此类内容更新到我的子视图的正确功能在哪里?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell_small") as! Cell
...
return cell
}
// In Cell
override func layoutSubviews() {
rankLabel.layer.cornerRadius = rankLabel.bounds.width/2
rankLabel.layer.masksToBounds = true
}
override func didMoveToSuperview() {
rankLabel.layer.cornerRadius = rankLabel.bounds.width/2
rankLabel.layer.masksToBounds = true
}
结果:
【问题讨论】:
-
你试过cellForRowAtIndexPath方法吗?
-
cellForRowAtIndexPath 在布局之前被调用,看我下面的回答,你可以使用 willDisplayCell
-
使用 rankLabel.clipsToBounds=YES;
-
如果我打印出 rankLabel 的尺寸,这对于我尝试过的所有东西来说都是错误的尺寸。但是如果我滑动表格视图,那么它会调用 updatefunctions 并且大小是正确的。
-
如果我想要一个圆形子视图,我之前在自动布局方面遇到了很多问题......
标签: ios uiview autolayout