Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hanishassim
Created February 23, 2020 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanishassim/7447b934595e69c379cd17b753d47611 to your computer and use it in GitHub Desktop.
Save hanishassim/7447b934595e69c379cd17b753d47611 to your computer and use it in GitHub Desktop.
Superclass to create rounded UIView (alternative to SmoothedView)
//
// RoundedCorners.swift
//
// Created by H on 23/02/2020.
// Copyright © 2020 H. All rights reserved.
//
import UIKit
protocol RoundedCorners {
var cornerRadius: CGFloat { get set }
func applyRoundedCorners()
}
extension RoundedCorners where Self: UIView {
func applyRoundedCorners() {
let bezierPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
let maskLayer = CAShapeLayer()
maskLayer.path = bezierPath.cgPath
layer.mask = maskLayer
}
}
public class BadgeView: UIView {
var cornerRadius: CGFloat = 20.0
public override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
public override func draw(_ rect: CGRect) {
applyRoundedCorners()
}
override public func layoutSubviews() {
super.layoutSubviews()
applyRoundedCorners()
}
}
extension BadgeView: RoundedCorners { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment