简体   繁体   中英

UIBarButton Item Won't Appear in Navigation Bar

I understand there are other questions like this but so far I have not been able to get an answer that works for so this is why I am writing this.

I right now have the following code to place a leftBarButtonItem:

   let user = FIRAuth.auth()?.currentUser
    var imageView: UIImageView?

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        setupNavBar()
    }

    func setupNavBar() {

        //left bar button item setup

        let url = URL(string: (self.user?.photoURL?.absoluteString)!)
        let data = try? Data(contentsOf: url!)
        imageView?.image = UIImage(data: data!)

        imageView?.layer.masksToBounds = true
        imageView?.layer.borderWidth = 1.5
        imageView?.layer.borderColor = UIColor.white.cgColor
        imageView?.layer.cornerRadius=(imageView?.bounds.width)! / 2

        let profileImageButton = UIBarButtonItem(image: UIImage(named: (self.user?.photoURL?.absoluteString)!), style: .plain, target: self, action:#selector(UserGroupsMainViewController.navLeftBarButtonTapped))

        self.navigationController?.navigationItem.leftBarButtonItem = profileImageButton
    }

    func navLeftBarButtonTapped() {

    } 

For some reason, the button does not appear on the navigation controller. I do know for sure that I have a URL that is not nil because I tested it in my print output. Any help would be appreciated. Thanks!

Just use navigationItem not navigationController.navigationItem

Ok, I just saw your problem.

You are using a URL as the "name" of an image. It doesn't work that way.

The function UIImage(named: "blah") will load an image from the app bundle stored with the name given.

If you want to download the image from a URL then you need to have a download task run in the background and then load the image once that's done.

Try using the AlamofireImage framework also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM