Skip to content

Commit

Permalink
Support for custom corner radius
Browse files Browse the repository at this point in the history
  • Loading branch information
anfriis committed Nov 7, 2016
1 parent 9a6ee9c commit 7b067c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 15 additions & 5 deletions DGRunkeeperSwitch/DGRunkeeperSwitch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import UIKit

open class DGRunkeeperSwitchRoundedLayer: CALayer {

override open var bounds: CGRect {
didSet { cornerRadius = bounds.height / 2.0 }
}

}

// MARK: - DGRunkeeperSwitch
Expand Down Expand Up @@ -64,7 +60,18 @@ open class DGRunkeeperSwitch: UIControl {
set { selectedBackgroundView.backgroundColor = newValue }
get { return selectedBackgroundView.backgroundColor }
}


@IBInspectable
// Set a custom corner radius or use the default
open var cornerRadius: CGFloat? {
didSet {
guard let cornerRadius = self.cornerRadius else { return }

self.layer.cornerRadius = cornerRadius
selectedBackgroundView.layer.cornerRadius = cornerRadius
}
}

@IBInspectable
open var titleColor: UIColor! {
didSet { titleLabels.forEach { $0.textColor = titleColor } }
Expand Down Expand Up @@ -160,6 +167,9 @@ open class DGRunkeeperSwitch: UIControl {
addGestureRecognizer(panGesture)

addObserver(self, forKeyPath: "selectedBackgroundView.frame", options: .new, context: nil)

// Set default corner radius
cornerRadius = bounds.height / 2.0
}

override open func awakeFromNib() {
Expand Down
2 changes: 2 additions & 0 deletions DGRunkeeperSwitchExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class ViewController: UIViewController {
runkeeperSwitch4.titleColor = .white
runkeeperSwitch4.selectedTitleColor = UIColor(red: 135/255.0, green: 227/255.0, blue: 120/255.0, alpha: 1.0)
runkeeperSwitch4.titleFont = UIFont(name: "HelveticaNeue-Light", size: 17.0)
// Custom corner radius
runkeeperSwitch4.cornerRadius = 8
}
}

Expand Down

0 comments on commit 7b067c3

Please sign in to comment.