Reputation: 21
I use the Users Personal Image as an TabBar Item like this:
func setUpProfileImage(){
let userID = Auth.auth().currentUser!.uid
let ref2 = Database.database().reference().child("users").child(userID)
ref2.child("profileImageUrl").observeSingleEvent(of: .value, with: { snapshot in
if let url = snapshot.value as? String {
URLSession.shared.dataTask(with: URL(string: url)!) { data, response, error in
if error == nil {
let image = UIImage(data: data!)
DispatchQueue.main.async {
if let lastTabBarButton = self.tabBar.subviews.last, let tabItemImageView = lastTabBarButton.subviews.first {
if let accountTabBarItem = self.tabBar.items?.last {
accountTabBarItem.selectedImage = nil
accountTabBarItem.image = nil
}
let imgView = UIImageView()
imgView.frame = tabItemImageView.frame
imgView.layer.cornerRadius = tabItemImageView.frame.height/2
imgView.layer.masksToBounds = true
imgView.contentMode = .scaleAspectFill
imgView.clipsToBounds = true
imgView.image = image
self.tabBar.subviews.last?.addSubview(imgView)
}
}
}
}.resume()
}
})
}
The image gets shown, but it is way to wide - is there a way to make the width the same as the height?
Upvotes: 0
Views: 92
Reputation: 4239
The frame you set needs to be adjusted, try setting the frame like this:
let imgView = UIImageView()
let frame = tabItemImageView.frame
imgView.frame = CGRect(
x: frame.origin.x + (frame.width / 2) - (frame.height / 2),
y: frame.origin.y,
width: frame.height,
height: frame.height
)
Upvotes: 1