gpt4 book ai didi

ios - 未在自定义 UIButton 上设置图像

转载 作者:行者123 更新时间:2023-11-30 11:02:16 25 4
gpt4 key购买 nike

所以我试图在我的应用程序中创建一个圆形导航栏按钮来保存用户图像。我采用了 UIButton 子类化并创建自己的自定义类的路线。

除了我尝试抓取图像的部分之外,一切都很好。我使用 alamofire 根据从 firebase 获取的 URL 获取图像,但按钮是蓝色的?没有图像

我的代码是否做错了什么

import UIKit
import Firebase
import AlamofireImage
import Alamofire

class CircularNavButton: UIButton {
var userHandle: DatabaseHandle = 0
var userRef: DatabaseReference?


override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

@objc func setupView(){
observeUser()
}


@objc func observeUser(){
self.userHandle = UserService.observeProfile(for: User.current, completion: { (userRef, user, nil) in
self.userRef = userRef
guard let userProfilePic = user?.profilePic else{
return
}
Alamofire.request(userProfilePic).responseImage { response in
debugPrint(response)
if let image = response.result.value {
self.setImage(image, for: .normal)
print("image downloaded: \(image)")
}
}
})
}

deinit {
userRef?.removeObserver(withHandle: userHandle)

}

}

下面是我将其添加到导航栏时的初始化

 @objc func setupCircularNavButton(){
let image = #imageLiteral(resourceName: "sports").withRenderingMode(.alwaysOriginal)

let customView = CircularNavButton(type: .system)
customView.addTarget(self, action: #selector(showMenu), for: .touchUpInside)
customView.imageView?.contentMode = .scaleToFill

customView.layer.cornerRadius = 15
customView.clipsToBounds = true

customView.widthAnchor.constraint(equalToConstant: 30).isActive = true
customView.heightAnchor.constraint(equalToConstant: 30).isActive = true

let barButtonItem = UIBarButtonItem(customView: customView)
navigationItem.rightBarButtonItems?.insert(barButtonItem, at: 0)
}

最佳答案

因此,当我像这样初始化按钮时,我可以通过将按钮类型设置为自定义来解决此问题

    let customView = CircularNavButton(type: .custom)

关于ios - 未在自定义 UIButton 上设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53198297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com