Common Navigation Bar iOS Swift 3
In android, they had Hamburger menu for the common menu.They will use fragment for the menu.But on iOS, we don't have the option.So we will try the below method to achieve that.First, we will create an XIB file for the menu.Then add the menu options in that file.In ViewController add the XIB file as a subview in the top of the view.
The class file as follows
Navigation.swift:
import UIKit
class Navigation: UIView {
@IBOutlet weak var mainTitleLabel: UILabel!
@IBOutlet weak var subTitleLabel: UILabel!
@IBOutlet weak var backBtn: UIButton!
@IBOutlet weak var rightBtn: UIButton!
@IBOutlet weak var addBtn: UIButton!
var callingViewController:AnyObject!
var callingNavigationController:AnyObject!
override internal func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
}
override internal func awakeFromNib(){
super.awakeFromNib()
mainTitleLabel.font = UIFont.boldSystemFont(ofSize: 18)
subTitleLabel.font = UIFont.systemFont(ofSize: 13) //UIFont(name: "Roboto-Regular", size: 12.0)
}
func setFrame(customFrame:CGRect, haveBackBtn:Bool, headerName:String, fromViewController:AnyObject ,fromNavigation:UINavigationController) {
if headerName == subtitle{
rightBtn.setImage(nil, for: .normal)
rightBtn.setBackgroundImage(#imageLiteral(resourceName: "ic_done"), for: .normal)
}
callingViewController = fromViewController
callingNavigationController = fromNavigation
subTitleLabel.text = headerName
mainTitleLabel.text = AppTitle
self.frame = customFrame
if haveBackBtn {
self.backBtn.isHidden = false
} else {
self.backBtn.isHidden = true
}
backBtn.addTarget(self, action: #selector(self.didTapOnBack(_sender:)), for: .touchUpInside)
}
// MARK:Button Back Action
func didTapOnBack(_sender:AnyObject) {
if ((self.viewController()?.isKind(of: ViewController.self))! {
} else {
let _ = callingNavigationController?.popViewController(animated: true)
}
}
}
Outlets as follows:
let header:Navigation = Bundle.main.loadNibNamed("Navigation", owner: nil, options: nil)![0] as! Navigation
header.setFrame(customFrame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60), haveBackBtn: false, headerName: "title", fromViewController: ViewController(), fromNavigation: self.navigationController!)
header.addBtn.addTarget(self, action: #selector(ViewController.didTapOnAdd), for: .touchUpInside)
header.rightBtn.addTarget(self, action: #selector(ViewController.didTapOnSearch(_:)), for: .touchUpInside)
self.view.addSubview(header)
You can replace your ViewController name with ViewControllerThen you have to add the xib as a subview in the requirable ViewController. haveBackBtn: false is used to show if back button is required then send it as true otherwise send it as false.If you had more buttons with different icons then use the addBtn and rightBtn buttons to show and hidden and also add the target from your view controller.If you want to override the backButton action it also possible.You can create an action inside your ViewController and override the method.
func didTapOnBack(_sender:AnyObject) {
}
If you overrode then add your View controller in the below code.Then it will be omitted to pop the viewController.You can do the actions inside your ViewController.
if ((self.viewController()?.isKind(of: ViewController.self))! {
} else {
let _ = callingNavigationController?.popViewController(animated: true)
}
Comments
Post a Comment