[英]Unstable text centering in a tableView cell
我在tableView Cell中使用居中文本的真實設備上有一個奇怪的問題。 在iPhone 6(iOS 12.3.1)上運行代碼時,代碼運行良好,文本在單元格中正確居中。 但是當我在iPhone7(iOS 12.3.1)上運行它時,文本在右邊是合理的。 當代碼在Xcode模擬器上運行時,它工作正常。
我正在使用Xcode 10.2.1並且Xcode沒有警告任何錯誤。 看起來文本居中的問題取決於設備(在兩個真實設備上運行時完全沒有錯誤)。
以下是我的代碼......
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// Set text label
cell.textLabel?.textColor = UIColor(red: 74/255, green: 88/255, blue: 205/255, alpha: 1)
cell.textLabel?.textAlignment = .center
// Set font and font size
cell.textLabel?.font = UIFont(name:"Helvetica Neue", size: 17)
}
有什么方法可以解決這個問題嗎?
嘗試使用cellForRowAt indexPath而不是willDisplay單元格
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:tableviewCell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath as IndexPath) as! tableviewCell
// Set text label
cell.textLabel?.textColor = UIColor(red: 74/255, green: 88/255, blue: 205/255, alpha: 1)
cell.textLabel?.textAlignment = .center
// Set font and font size
cell.textLabel?.font = UIFont(name:"Helvetica Neue", size: 17)
return cell
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.