[英]Image not being set on custom UIButton
So I am trying to create a circular navbar button in my app that holds a users image. 因此,我试图在我的应用中创建一个保存用户图像的圆形导航栏按钮。 I went the route of subclassing UIButton and creating my own custom class.
我走了继承UIButton并创建自己的自定义类的路线。
All is well except for the part where I try to grab the image. 除了我尝试抓取图像的那部分以外,其他一切都很好。 I use alamofire to get the image based off of the URL that i get from firebase however the button is blue?
我使用alamofire根据从Firebase获取的URL获取图像,但是按钮为蓝色? with no image
没有图像
Have I done anything wrong in my code 我在代码中做错了什么吗
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)
}
}
Below is the init for when I add it to my navbar 以下是将其添加到导航栏中时的初始化
@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)
}
因此,我可以通过在初始化时将按钮类型设置为custom来解决此问题
let customView = CircularNavButton(type: .custom)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.