[Solved] Logout button in every viewcontroller in swift 5


I’ve created an UILibraryFunction.swift file.

class UILibraryFunction: UIViewController {

var navBar:UINavigationBar = UINavigationBar()
var navItem = UINavigationItem(title: "SomeTitle")

var screenWidth:CGFloat = 0
var screenHeight:CGFloat = 0

var NameHeight:CGFloat = 0
var NameWidth:CGFloat = 0

override func viewDidLoad() {
    super.viewDidLoad()

    let screenSize: CGRect = UIScreen.main.bounds

    screenWidth = screenSize.width
    screenHeight = screenSize.height


    NameHeight = screenHeight * 0.09
    NameWidth = screenWidth
    navBar = UINavigationBar(frame: CGRect(x: 0, y: 30, width: NameWidth, height: NameHeight))
    self.view.addSubview(navBar)

    navBar.setItems([navItem], animated: false)

}


//---- Right Bar Button -----
func rightBarButton() {
    let rightBarButton = UIBarButtonItem(title: "Logout", style: UIBarButtonItem.Style.plain, target: self, action: #selector(Lge3Generell.Logout(_:)))
    navItem.rightBarButtonItem = rightBarButton

}

//--- Left Bar Button -----
func leftBarButton() {

    let leftBarButton = UIBarButtonItem(title: "Home", style: UIBarButtonItem.Style.done, target: self, action: #selector(self.GoToHome(_:)))
    navItem.leftBarButtonItem = leftBarButton

}

//----- UI Bar Title ----
func setTitle(BarTitle:String) {
    navItem = UINavigationItem(title: BarTitle)
    return navBar.setItems([navItem], animated: false)
}

}

Now I call this 3 function in all of my ViewController ‘s ViewDidLoad like below:-

class General: UILibraryFunction {


override func viewDidLoad() {
    super.viewDidLoad()

    //-- top bar ------
    setTitle(BarTitle: "Header")
    rightBarButton()
    leftBarButton()

}
}

solved Logout button in every viewcontroller in swift 5