IOS navigation bar button image

0

I need to add two buttons in the navigation bar of my app, but what I need to use an external image (already imported to the project) does not appear.

    override func viewDidLoad() {
        super.viewDidLoad()

         //Esse botão aparece (icone da própria biblioteca)

        let rightShareBarButtonItem:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.action, target: self, action: #selector(ViewController.shareTapped))

        //Esse ícone não aparece, estou tentando carregar uma imagem interna - já importada ao projeto

        let rightInfoBarButtonItem:UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "info.png"), style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.infoTapped))

        self.navigationItem.setRightBarButtonItems([rightInfoBarButtonItem, rightShareBarButtonItem], animated: true)
    }
    
asked by anonymous 06.10.2017 / 12:54

1 answer

2

Does your ViewController have a NavigationController ? Because if it does not, its buttons will not appear on the screen.

How to add a navigationController to your viewController via code .

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds)

    let controller = ViewController()

    let navigationController = UINavigationController(rootViewController: controller)

    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()


    return true
}

I now saw storyboard .

Thenyoushouldstaylikethis

NowinsideyourViewControllerindependentifitisviacode,orbystoryboard,thisshouldwork.

letimage=UIImageView(image:#imageLiteral(resourceName:"image"))

    let rightButton = UIBarButtonItem(image: image.image, style: .plain, target: nil, action: nil)

    navigationItem.setRightBarButton(rightButton, animated: true)
    
09.10.2017 / 01:49