如何快速更新TableView单元格约束

时间:2020-04-09 19:35:05

标签: swift layout rotation constraints tableview

当约束处于垂直位置(纵向)时,约束是正确的;但是当电话处于水平位置(横向)时,其中的一些约束会被更新,而某些约束则不会。

在照片中很清楚

肖像 enter image description here

风景 enter image description here

这是我的代码:

override func willAnimateRotation(to toInterfaceOrientation:      UIInterfaceOrientation, duration: TimeInterval)
{
    DispatchQueue.main.async {
        self.Table_View.reloadData()
        self.Table_View.setNeedsLayout()
        self.Table_View.layoutIfNeeded()
    }

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let Cell = tableView.dequeueReusableCell(withIdentifier: "sdr", for: indexPath) as! HistoryPagesCell
    let bgColorView = UIView()
    bgColorView.backgroundColor = UIColor.clear
    Cell.selectedBackgroundView = bgColorView
    Cell.PageCleanurl.text = HistoryArray[indexPath.row].title
    Cell.PageLasturl.text = HistoryArray[indexPath.row].lasturl
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "HH-mm"
    let date = dateFormatter.string(from: HistoryArray[indexPath.row].date!)
    let str = date
    let replaced = str.replacingOccurrences(of: "-", with: ":")
    Cell.Date.text = replaced
    Cell.ViewMain.translatesAutoresizingMaskIntoConstraints = false
    Cell.ViewMain.centerXAnchor.constraint(equalTo: Cell.contentView.centerXAnchor).isActive = true
    Cell.ViewMain.centerYAnchor.constraint(equalTo: Cell.contentView.centerYAnchor).isActive = true
    Cell.ViewMain.heightAnchor.constraint(equalToConstant: 43).isActive = true
    Cell.ViewMain.widthAnchor.constraint(equalToConstant: view.frame.size.width - 10).isActive = true
    Cell.Date.translatesAutoresizingMaskIntoConstraints = false
    Cell.Date.centerYAnchor.constraint(equalTo: Cell.ViewMain.centerYAnchor).isActive = true
    Cell.Date.rightAnchor.constraint(equalTo: Cell.ViewMain.rightAnchor, constant: -14).isActive = true
    return Cell
}

0 个答案:

没有答案