我有一个UITabBarController
,有5个标签。
我想在每个标签下放置一个GIF
文件。
我使用3rd party API (UIImage
extension) called SwiftGif
来使用gif,所以问题也可以这样问:
"如何将UIImage
作为UITabBar
"
但我想更具体地帮助你更好地理解。
答案 0 :(得分:0)
尝试使用此更改tabBar的背景self.tabBarController?.tabBar.backgroundImage = UIImageView(image: UIImage.gifWithName("yourGifName"))
请记住,此语法来自Swift 3 - 如果您使用的是Swift 2,则可能需要稍微更改一下
答案 1 :(得分:0)
快捷键4: 标签栏的类,然后设置如下图像:
import UIKit
import SwiftGifOrigin ///to install that pod: https://github.com/bahlo/SwiftGifCreate
class TabbarVC: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let myTabBarItem1 = (self.tabBar.items?[0])! as UITabBarItem
myTabBarItem1.image = UIImage(named: "your unselected image")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem1.selectedImage = UIImage.gif(asset: "your gif")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem1.title = ""
myTabBarItem1.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
}
}
答案 2 :(得分:0)
这是我的解决方案,只需在Tabbar类中为所选图像设置gif。
import UIKit
class TabbarVC: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
//Set Selected image for all items, I'm doing only for item 1
let myTabBarItem1 = (self.tabBar.items?[0])! as UITabBarItem
myTabBarItem1.selectedImage = UIImage.gifImageWithName("image name")
}
}
这是从Gif
加载URL/Bundle/Name
的扩展名。
extension UIImage {
public class func gifImageWithData(_ data: Data) -> UIImage? {
guard let source = CGImageSourceCreateWithData(data as CFData, nil) else {
print("image doesn't exist")
return nil
}
return UIImage.animatedImageWithSource(source)
}
public class func gifImageWithURL(_ gifUrl:String) -> UIImage? {
guard let bundleURL:URL = URL(string: gifUrl)
else {
print("image named \"\(gifUrl)\" doesn't exist")
return nil
}
guard let imageData = try? Data(contentsOf: bundleURL) else {
print("image named \"\(gifUrl)\" into NSData")
return nil
}
return gifImageWithData(imageData)
}
public class func gifImageWithName(_ name: String) -> UIImage? {
guard let bundleURL = Bundle.main
.url(forResource: name, withExtension: "gif") else {
print("SwiftGif: This image named \"\(name)\" does not exist")
return nil
}
guard let imageData = try? Data(contentsOf: bundleURL) else {
print("SwiftGif: Cannot turn image named \"\(name)\" into NSData")
return nil
}
return gifImageWithData(imageData)
}