[英]UIBarButton Item Won't Appear in Navigation Bar
我知道還有其他類似的問題,但到目前為止我還沒有得到一個有效的答案,所以這就是我寫這篇文章的原因。
我現在有以下代碼來放置一個 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() {
}
出於某種原因,該按鈕沒有出現在導航控制器上。 我確實知道我有一個不為零的 URL,因為我在打印輸出中對其進行了測試。 任何幫助,將不勝感激。 謝謝!
只需使用navigationItem
而不是navigationController.navigationItem
好的,我剛看到你的問題。
您正在使用 URL 作為圖像的“名稱”。 它不會那樣工作。
函數UIImage(named: "blah")
將從存儲的應用程序包中加載一個圖像,該圖像以給定的名稱存儲。
如果要從 URL 下載圖像,則需要在后台運行下載任務,然后在完成后加載圖像。
也嘗試使用 AlamofireImage 框架。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.