问题描述
我有一个自定义 UIView
和一个表视图,它们都是以编程方式创建的。我的自定义 UIView
涉及一个 UIBezierPath
来创建它的形状。
我将此 UIView
添加到表视图 tableHeaderView 中,它具有动态高度。当我将我的应用程序从纵向旋转到横向时,曲线不会更新。任何帮助将不胜感激。
class ExplanationCurvedHeaderView: UIView {
public lazy var headerView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.autoresizesSubviews = true
view.autoresizingMask = [.flexibleWidth,.flexibleHeight]
//view.backgroundColor = .gray
return view
}()
private lazy var headerLabel: UILabel = {
let headerLabel = UILabel()
headerLabel.translatesAutoresizingMaskIntoConstraints = false
headerLabel.numberOfLines = 0
headerLabel.lineBreakMode = .byWordWrapping
headerLabel.textAlignment = .center
headerLabel.font = .systemFont(ofSize: 18,weight: .semibold)
headerLabel.textColor = .black
headerLabel.text = "Hello this is a test Hello this is a testHello this is a testHello this is a test Hello this is a test Hello this is a test Hello this is a test Hello this is a test Hello this is a test"
return headerLabel
}()
public lazy var imageIcon: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit
imageView.image = UIImage(systemName: "exclamationmark.triangle.fill")
return imageView
}()
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
createCurve()
}
func configure() {
addSubview(headerView)
headerView.addSubview(headerLabel)
headerView.addSubview(imageIcon)
NSLayoutConstraint.activate([
headerView.topAnchor.constraint(equalTo: topAnchor,constant: -44),headerView.leadingAnchor.constraint(equalTo: leadingAnchor),headerView.trailingAnchor.constraint(equalTo: trailingAnchor),headerView.bottomAnchor.constraint(equalTo: bottomAnchor,constant: -80),headerLabel.topAnchor.constraint(equalTo: headerView.topAnchor,constant: 75),headerLabel.leadingAnchor.constraint(equalTo: headerView.leadingAnchor,constant: 20),headerLabel.trailingAnchor.constraint(equalTo: headerView.trailingAnchor,constant: -20),headerLabel.bottomAnchor.constraint(equalTo: headerView.bottomAnchor,imageIcon.centerXAnchor.constraint(equalTo: centerXAnchor),imageIcon.topAnchor.constraint(equalTo: headerView.bottomAnchor,constant: 10),imageIcon.heightAnchor.constraint(equalToConstant: 64),imageIcon.widthAnchor.constraint(equalToConstant: 64)
])
}
func createCurve() {
let shapeLayer = CAShapeLayer(layer: headerView.layer)
shapeLayer.path = pathForCurvedView().cgPath
//shapeLayer.frame = self.bounds
shapeLayer.frame = headerView.bounds
shapeLayer.fillColor = UIColor.lightGray.cgColor
headerView.layer.mask = shapeLayer.mask
headerView.layer.insertSublayer(shapeLayer,at: 0)
}
func pathForCurvedView() -> UIBezierPath {
let path = UIBezierPath()
path.move(to: CGPoint(x: 0,y: 0))
path.addLine(to: CGPoint(x: UIScreen.main.bounds.maxX,y: headerView.frame.height + 30))
path.addQuadCurve(to: CGPoint(x: UIScreen.main.bounds.minX,y: headerView.frame.height + 30),controlPoint: CGPoint(x: UIScreen.main.bounds.midX,y: headerView.frame.height + 70))
print("header view height:",headerView.frame.height)
print("header label systemLayou:",headerLabel.systemLayoutSizefitting(UIView.layoutFittingExpandedSize))
path.close()
return path
}
}
这是视图控制器代码:
import UIKit
class ViewController: UIViewController {
private lazy var headerView: ExplanationCurvedHeaderView = {
let curvedView = ExplanationCurvedHeaderView(frame: .zero)
return curvedView
}()
var arrayValues: [String] = []
private lazy var tableView: UITableView = {
let tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.delegate = self
tableView.dataSource = self
tableView.frame = view.safeAreaLayoutGuide.layoutFrame
return tableView
}()
/*
Tried few things in this override but no correct results
override func viewWillTransition(to size: CGSize,with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size,with: coordinator)
coordinator.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) -> Void in
self.headerView.headerView.setNeedsLayout()
self.headerView.createCurve()
self.tableView.setNeedsLayout()
self.tableView.setNeedsdisplay()
})
} */
override func viewDidLoad() {
for x in 1...6 {
if x.isMultiple(of: 2) {
arrayValues.append("Get To Invesin InvestinGet To Kn Get e o Kn Get eo Kn Get eo")
} else {
arrayValues.append("Get To Invesin InvestinGet To Kn Get e o Kn Get eo Kn Get eo Kn Get eo Kn Get eo Kn Get eo Kn Get eo Kn Get eo Kn Get e Get To Invesin InvestinGetGet To Invesin InvestinGetGet To Invesin InvestinGetGet To Invesin InvestinGetGet To Invesin InvestinGet nvestinGetGenvestinGetGenvestinGetGenvestinGetGenvestinGetGe")
}
}
tableView.register(CustomTableViewCell.self,forCellReuseIdentifier: "cell")
//tableView.tableHeaderView = headerView
view.addSubview(tableView)
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.topAnchor),tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),])
tableView.tableHeaderView = headerView
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if let headerView = self.tableView.tableHeaderView {
let headerViewFrame = headerView.frame
let height = headerView.systemLayoutSizefitting(headerViewFrame.size,withHorizontalFittingPriority: UILayoutPriority.defaultHigh,verticalFittingPriority: UILayoutPriority.defaultLow).height
var headerFrame = headerView.frame
if height != headerFrame.size.height {
headerFrame.size.height = height
headerView.frame = headerFrame
self.tableView.tableHeaderView = headerView
}
}
headerView.layoutIfNeeded()
tableView.beginUpdates()
tableView.endUpdates()
}
}
extension ViewController: UITableViewDataSource,UITableViewDelegate {
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return arrayValues.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as? CustomTableViewCell else {
return UITableViewCell()
}
cell.setValue(value: arrayValues[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView,estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastnormalMagnitude
}
}
这也出现在控制台e:
2021-04-19 22:35:19.292199-0400 HeaderViewCurve[62191:1769385] [LayoutConstraints] 无法同时满足约束。 可能以下列表中的至少一项约束是您不想要的。 试试这个: (1) 查看每个约束并尝试找出您不期望的; (2) 找到添加不需要的约束或约束的代码并修复它。 ( "<0x6000022fac60 v: names:><0x6000022fad50 uilabel:0x7fdce741ce10.bottom="=" uiview:0x7fdce741cca0.bottom><0x6000022fab20 v: names:><0x6000022fac10 uiview:0x7fdce741cca0.bottom="=" headerviewcurve.explanationcurvedheaderview:0x7fdce740cf60.bottom><0x6000022f0460 headerviewcurve.explanationcurvedheaderview:0x7fdce740cf60.height="=">0x6000022f0460>0x6000022fac10>0x6000022fab20>0x6000022fad50>0x6000022fac60>
将尝试通过打破约束来恢复<0x6000022fad50 uilabel:0x7fdce741ce10.bottom="=" uiview:0x7fdce741cca0.bottom>0x6000022fad50>
在处创建一个符号断点以在调试器中捕获它。
将尝试通过打破约束来恢复<0x6000022fac10 uiview:0x7fdce741cca0.bottom="=" headerviewcurve.explanationcurvedheaderview:0x7fdce740cf60.bottom>0x6000022fac10>
在处创建一个符号断点以在调试器中捕获它。
解决方法
根据的建议,我可以通过在视图控制器中添加它来解决我的问题
>