在自定義tableView單元格中,我想創建頂部帶圓角的UIView。下面是代碼,其中我設置的UIView的制約圓角改變UIView大小
headerView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 8).isActive = true
headerView.topAnchor.constraint(equalTo: self.topAnchor, constant: 4).isActive = true
headerView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -8).isActive = true
headerView.heightAnchor.constraint(equalToConstant: 40).isActive = true
這部分的波紋管設置的觀點我已經做了圓角以下部分:
self.layoutIfNeeded()
let rectShape = CAShapeLayer()
rectShape.bounds = headerView.layer.frame
rectShape.position = headerView.center
rectShape.path = UIBezierPath(roundedRect: headerView.layer.bounds, byRoundingCorners: [ .topLeft, .topRight], cornerRadii: CGSize(width: 10, height: 10)).cgPath
headerView.layer.backgroundColor = UIColor.green.cgColor
headerView.layer.mask = rectShape
的問題是,當這個代碼塊用來設置圓角視圖的大小是變化的。下面是輸出
而結果沒有的這部分代碼如下:
我的問題是,爲什麼視圖大小變化?我做錯了什麼?
我不確定,但我認爲這可能是因爲您正在更改視圖的「圖層」,從而影響自動佈局約束。如果你想要做的只是在視圖的四角,那就沒有必要使用'CAShapeLayer' –
我想只在頂部有圓角,所以這就是我使用CAShapeLayer的原因 –