Created
May 1, 2018 10:02
-
-
Save alfian0/6752456d409fbb0e9a7f094e69cff70a to your computer and use it in GitHub Desktop.
Rounded tableView section
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { | |
| let color = cell.backgroundColor?.cgColor | |
| let cornerRadius: CGFloat = 5 | |
| cell.backgroundColor = .clear | |
| let layer = CAShapeLayer() | |
| let pathRef = CGMutablePath() | |
| let bounds = cell.bounds.insetBy(dx: 20, dy: 0) | |
| var addLine = false | |
| if indexPath.row == 0 && indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 { | |
| pathRef.__addRoundedRect(transform: nil, rect: bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius) | |
| } else if indexPath.row == 0 { | |
| pathRef.move(to: .init(x: bounds.minX, y: bounds.maxY)) | |
| pathRef.addArc(tangent1End: .init(x: bounds.minX, y: bounds.minY), tangent2End: .init(x: bounds.midX, y: bounds.minY), radius: cornerRadius) | |
| pathRef.addArc(tangent1End: .init(x: bounds.maxX, y: bounds.minY), tangent2End: .init(x: bounds.maxX, y: bounds.midY), radius: cornerRadius) | |
| pathRef.addLine(to: .init(x: bounds.maxX, y: bounds.maxY)) | |
| addLine = true | |
| } else if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 { | |
| pathRef.move(to: .init(x: bounds.minX, y: bounds.minY)) | |
| pathRef.addArc(tangent1End: .init(x: bounds.minX, y: bounds.maxY), tangent2End: .init(x: bounds.midX, y: bounds.maxY), radius: cornerRadius) | |
| pathRef.addArc(tangent1End: .init(x: bounds.maxX, y: bounds.maxY), tangent2End: .init(x: bounds.maxX, y: bounds.midY), radius: cornerRadius) | |
| pathRef.addLine(to: .init(x: bounds.maxX, y: bounds.minY)) | |
| } else { | |
| pathRef.addRect(bounds) | |
| addLine = true | |
| } | |
| layer.path = pathRef | |
| layer.fillColor = color ?? UIColor(white: 1, alpha: 0.8).cgColor | |
| if (addLine == true) { | |
| let lineLayer = CALayer() | |
| let lineHeight = 1.0 / UIScreen.main.scale | |
| lineLayer.frame = CGRect(x: bounds.minX + 10, y: bounds.size.height - lineHeight, width: bounds.size.width - 10, height: lineHeight) | |
| lineLayer.backgroundColor = tableView.separatorColor?.cgColor | |
| layer.addSublayer(lineLayer) | |
| } | |
| let v = UIView(frame: bounds) | |
| v.layer.insertSublayer(layer, at: 0) | |
| v.backgroundColor = .clear | |
| cell.backgroundView = v | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { | |
| let color = view.backgroundColor?.cgColor | |
| view.backgroundColor = UIColor.clear | |
| let cornerRadius: CGFloat = 5 | |
| let layer = CAShapeLayer() | |
| let pathRef = CGMutablePath() | |
| let bounds = view.bounds.insetBy(dx: 20, dy: 0) | |
| pathRef.move(to: .init(x: bounds.minX, y: bounds.maxY)) | |
| pathRef.addArc(tangent1End: .init(x: bounds.minX, y: bounds.minY), tangent2End: .init(x: bounds.midX, y: bounds.minY), radius: cornerRadius) | |
| pathRef.addArc(tangent1End: .init(x: bounds.maxX, y: bounds.minY), tangent2End: .init(x: bounds.maxX, y: bounds.midY), radius: cornerRadius) | |
| pathRef.addLine(to: .init(x: bounds.maxX, y: bounds.maxY)) | |
| layer.path = pathRef | |
| layer.fillColor = color ?? UIColor(white: 1, alpha: 0.8).cgColor | |
| let lineLayer = CALayer() | |
| let lineHeight = 1.0 / UIScreen.main.scale | |
| lineLayer.frame = CGRect(x: bounds.minX, y: bounds.size.height - lineHeight, width: bounds.size.width, height: lineHeight) | |
| lineLayer.backgroundColor = tableView.separatorColor?.cgColor | |
| layer.addSublayer(lineLayer) | |
| view.layer.insertSublayer(layer, at: 0) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment