Skip to content

Instantly share code, notes, and snippets.

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 vizbee/aa12ca83721fb22600f7d064c6d6710f to your computer and use it in GitHub Desktop.
Save vizbee/aa12ca83721fb22600f7d064c6d6710f to your computer and use it in GitHub Desktop.

Add CastIcon to a ViewController or NavigationBar by making the cast button's hit region bigger without making the cast button image itself larger

When to use this approach

  • When CastButton's hit region needs bigger

Instructions

  • Step 1 - Subclass the VZBCastButton and override the backgroundRect as shown in the 2-VizbeeCastButton.swift snippet
  • Step 2 - Add VizbeeCastButton to the NavigationBar as shown in the 3-InstructionsToAddVizbeeCastButtonToNavigationbar.swift snippet
  • Step 2.1 - Add VizbeeCastButton to the UIView as shown in the 4-InstructionsToAddVizbeeCastButtonToUIView.swift snippet
import VizbeeKit
class VizbeeCastButton: VZBCastButton {
required public init?(coder: NSCoder) { fatalError("init(coder:) not implemented") }
var backgroundRectRatio = 0.5
@objc init(with delegate: VZBCurrentVideoDelegate?, for viewController: UIViewController?, backgroundRectRatio: Double) {
super.init(frame: CGRect( x: 0, y: 0, width: 24, height: 24 ) )
self.backgroundRectRatio = backgroundRectRatio
backgroundColor = .yellow // make it to .clear for a transparent background
layer.cornerRadius = self.frame.size.width / 2
self.associatedViewController = viewController
self.delegate = delegate
self .addTarget(self, action: #selector(onCastIconClick), for: .touchUpInside)
}
override func backgroundRect(forBounds bounds: CGRect) -> CGRect {
var backgroundRect = super.backgroundRect(forBounds:bounds)
backgroundRect.size = CGSize( width: self.frame.size.width * backgroundRectRatio,
height: self.frame.size.height * backgroundRectRatio )
backgroundRect.origin = CGPoint( x: (self.frame.size.width - backgroundRect.size.width) / 2,
y: (self.frame.size.height - backgroundRect.size.height) / 2 )
return backgroundRect
}
@objc func onCastIconClick() {
self.performTap()
}
}
override func viewDidLoad() {
super.viewDidLoad()
...
// [Vizbee Begin] - Cast Icon
let vizbeeCastButton = VizbeeCastButton(with: nil, for: self, backgroundRectRatio: 0.5)
let barItemWithCastButton = UIBarButtonItem( customView: vizbeeCastButton )
navigationItem.rightBarButtonItem = barItemWithCastButton
// [Vizbee End] - Cast Icon
...
}
override func viewDidLoad() {
super.viewDidLoad()
...
// [Vizbee Begin] - Cast Icon
let vizbeeCastButton = VizbeeCastButton( with: nil, for: controller,
backgroundRectRatio: 0.5)
self.view.addSubview(vizbeeCastButton)
// add layoutconstraint as required
// [Vizbee End] - Cast Icon
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment