Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue on navigation bar image #197

Open
Zeeshan90 opened this issue Jun 9, 2022 · 0 comments
Open

Issue on navigation bar image #197

Zeeshan90 opened this issue Jun 9, 2022 · 0 comments

Comments

@Zeeshan90
Copy link

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
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant