Skip to content

Instantly share code, notes, and snippets.

@aybekckaya
Created June 26, 2018 00:29
Show Gist options
  • Save aybekckaya/544220d349870d4605b0b8b7ed464253 to your computer and use it in GitHub Desktop.
Save aybekckaya/544220d349870d4605b0b8b7ed464253 to your computer and use it in GitHub Desktop.
enum NavigationBarType {
case closeOnRight
}
class BaseViewController: UIViewController {
fileprivate static let barButtonSize = CGSize(width: 30, height: 30)
fileprivate enum NavigationBarButtonType {
case closeBtn
func view(vc:BaseViewController)->UIBarButtonItem {
switch self {
case .closeBtn:
let customView:UIView = UIView()
customView.frame = CGRect(x: 0, y: 0, width: BaseViewController.barButtonSize.width, height: BaseViewController.barButtonSize.height)
let image:UIImage = #imageLiteral(resourceName: "checkoutNavClose")
let btn = UIButton(type: UIButtonType.custom)
btn.frame = CGRect(x: 0, y: 0, width: customView.frame.size.width, height: customView.frame.size.height)
btn.setImage(image, for: UIControlState.normal)
btn.center = CGPoint(x: customView.frame.size.width / 2, y: customView.frame.size.height / 2 )
btn.addTarget(vc, action: #selector(dismissViewController), for: UIControlEvents.touchUpInside)
customView.addSubview(btn)
return UIBarButtonItem(customView: customView)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
updateNavigationBar()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
// Navigation Bar
extension BaseViewController {
func updateNavigationBar() {
switch self {
case is CustomTransitionVCDetail: navigationBarWithCloseOnRight()
default: break
}
}
private func navigationBarWithCloseOnRight () {
let btnItemRight = NavigationBarButtonType.closeBtn.view(vc: self)
self.navigationItem.rightBarButtonItems = [btnItemRight]
}
}
// Navigation Bar Button Blue Prints
extension BaseViewController {
@objc func dismissViewController () {
self.dismiss(animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment