Open
Description
I have an image in my navigation bar, when I get login into the application the logo is perfectly centered aligned. But when I click on the side menu and try to open the side menu my logo gets alignment changed, logo moved in the left direction. Please look into it.
Here is the code to open the side menu:
let vc: SideMenuViewController = UIStoryboard.controller()
vc.sideMenu = Global.shared.drawer
sideMenuController?.leftViewController = vc
sideMenuController?.showLeftViewAnimated()
Let me share the code for the center that aligns the image.
func addLogoToNavigationBarItem(isHomeVC: Bool? = false,ispreLogin:Bool! = false) {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.heightAnchor.constraint(equalToConstant: 30).isActive = true
imageView.contentMode = .scaleAspectFit
imageView.image = UIImage(named: "ic_logo")
// In order to center the title view image no matter what buttons there are, do not set the
// image view as title view, because it doesn't work. If there is only one button, the image
// will not be aligned. Instead, a content view is set as title view, then the image view is
// added as child of the content view. Finally, using constraints the image view is aligned
// inside its parent.
let contentView = UIView()
self.navigationItem.titleView = contentView
self.navigationItem.titleView?.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
imageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
// If it is not a home view controller
if !isHomeVC!{
enableSwipeBackFromLeftEdge()
if !ispreLogin && sideMenuController != nil{
sideMenuController!.isLeftViewSwipeGestureEnabled = false
}
let leftBarButtonItem = UIBarButtonItem()
leftBarButtonItem.image = UIImage(named: AssetImages.LEFT_ARROW)
leftBarButtonItem.imageInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 0)
leftBarButtonItem.action = #selector(barButtonAction)
leftBarButtonItem.target = self
self.navigationItem.setLeftBarButton(leftBarButtonItem, animated: true)
}else{
sideMenuController!.isLeftViewSwipeGestureEnabled = true
}
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.shadowColor = .clear
appearance.backgroundImage = UIImage(named: AssetImages.BACKGROUND_IMG)
navigationController?.navigationBar.tintColor = .darkGray
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
} else {
// Fallback on earlier versions
self.navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.setBackgroundImage(UIImage(named: AssetImages.BACKGROUND_IMG), for: .default)
navigationController?.navigationBar.tintColor = .darkGray
}
}
Metadata
Assignees
Labels
No labels