Click here to Skip to main content
15,565,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to build a Cocoa Touch Class that is assigned to my Navigation Controller in my Storyboard to display an image in the Navigation Bar in the center. The app builds but the image is not displayed and I can figure out why.

Swift
class NavBarImg: UINavigationController {
    
    
    required init(coder aDecoder: NSCoder){
            super.init(coder: aDecoder)!
            setupView()
    }
    
    func setupView()
    {
        if navigationController == nil{
            return
        }
        
        let image = #imageLiteral(resourceName: "BarTabsNavLogoWhite")
        let imageView = UIImageView(image: image)
        
        let bannerWidth = navigationBar.frame.size.width
        let bannerHeight = navigationBar.frame.size.height
        
        let bannerX = bannerWidth / 2 - image.size.width / 2
        let bannerY = bannerHeight / 2 - image.size.height / 2
        
        imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
        imageView.contentMode = .scaleAspectFit
        
        self.navigationItem.titleView = imageView  
        
    }

    }


What I have tried:

- Assigned custom class to in storyboard
- Currently have it working as an extension to UIViewController but the image in the title screen loads every time. Looking to make it more fluid looking and potentially get it to work with @IBDesignable.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900