我有一個 UITableView,它有一個包含 UIImageView 的 UITableViewCell。
約束的設定使得 UIImageView 在頂部和側面具有 20 個填充點,并且大小比為 1:1,因此無論設備寬度如何,UIImageView 都將始終是方形的。
我對 UIImageView 應用了一個cornerRadius,所以影像是圓形的。
但是....自動布局似乎在第一次加載時不起作用。但在第一次加載后,它作業得很好。
我已經嘗試了 setNeedsLayout 或 layoutIfNeeded 的所有已知組合 - 在 UITableViewCell 和 UITableView 代碼中。第一次加載時沒有任何效果。
請幫忙!
代碼如下所示:
class CircularProfileCell: UITableViewCell {
@IBOutlet weak var circularView: UIView!
func setup() {
circularView.layer.cornerRadius = circularView.bounds.height / 2
}
}
class CircularProfileVC: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.separatorStyle = .none
self.tableView.register(UINib(nibName: "CircularProfileCell", bundle: nil), forCellReuseIdentifier: "CircularProfileCell")
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CircularProfileCell", for: indexPath) as! CircularProfileCell
cell.setup()
return cell
}
}
設定如下所示:

uj5u.com熱心網友回復:
因為圓角半徑是一個layer屬性,所以它并不總是能很好地適應自動布局。另外,我猜你用視圖的框架屬性設定它(即imageView.layer.cornerRadius = imageView.bounds.height/2)。因此,您應該嘗試layoutSubviews()在單元格的功能上設定角半徑。這將確保呈現正確的大小
override func layoutSubviews() {
super.layoutSubviews()
imageView.layer.cornerRadius = imageView.bounds.height/2
...
}
uj5u.com熱心網友回復:
這只發生在 tableView.separatorStyle = .none
所以要修復它,我只需打開分隔符,但將分隔符顏色設定為清除
self.tableView.separatorStyle = .singleLine
self.tableView.separatorColor = UIColor.clear
感謝@CloudBalacing 的幫助。關于這個問題的更多資訊在這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/408273.html
標籤:
上一篇:保持視圖縱向但讓iOS隨手機旋轉
下一篇:如何將針旋轉半圓?
