繁体   English   中英

UITabBar背景图像的图像缩放

[英]image scaling for UITabBar background image

我在我的应用程序中创建了UITabBarController。

然后在viewDidLoad()我想更改UITabBar背景图像。 这是我试图让它工作的代码:

class MainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UITabBar.appearance().translucent = false
        UITabBar.appearance().backgroundColor = UIColor.clearColor()
        UITabBar.appearance().backgroundImage = UIImage(named: "tabbar_background")
        UITabBar.appearance().contentMode = .ScaleAspectFit
    }
}

但结果不正确( 图像 )。 有人可以帮助我填充整个tabbar框架吗?

尝试将image调整为标签栏大小。 或添加imageViewtabBar作为subview ,然后利用图像中imageView

TabBarController子类化并在其中添加imageview

var bgView: UIImageView = UIImageView(image: UIImage(named: "tabBarBackground.png"))
bgView.frame = CGRectMake(0, 420, 320, 60)//you might need to modify this frame to your tabbar frame
self.view.addSubview(bgView)

以下是带有自定义图标的自定义半透明标签栏图像的代码。 您需要通过变量直接引用标签栏。

斯威夫特3

private func setupTabBar() {
    print ("Setting up the Tab Bar and Items")

    tabBarView.clipsToBounds = true
    tabBarView.backgroundImage = UIImage()

    let bgView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "TabBarBackground"))
    bgView.frame = tabBarView.bounds
    tabBarView.addSubview(bgView)

    tabBarView.tintColor = UIColor.white
    UITabBar.appearance().tintColor = UIColor.gray
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .normal)
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.gray], for: .selected)

    tabBarLimitsItemView.image      = #imageLiteral(resourceName: "TabIconLimits").withRenderingMode(UIImageRenderingMode.alwaysOriginal)
    tabBarControlsItemView.image    = #imageLiteral(resourceName: "TabIconControls").withRenderingMode(UIImageRenderingMode.alwaysOriginal)
    tabBarPayRulesItemView.image    = #imageLiteral(resourceName: "TabIconPayRules").withRenderingMode(UIImageRenderingMode.alwaysOriginal)
    tabBarSettingsItemView.image    = #imageLiteral(resourceName: "TabIconSettings").withRenderingMode(UIImageRenderingMode.alwaysOriginal)
}

这对我来说很快3

class MyTabBarController: UITabBarController, UINavigationControllerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    let backgroundImage = UIImageView(image: UIImage(named: "gray background"))
    backgroundImage.frame = backgroundImage.bounds
    self.view.addSubview(backgroundImage)


}

我使用clipsToBounds解决了这个问题。
这是我的例子:

let tabBar = self.tabBar
tabBar.backgroundImage = UIImage()
tabBar.clipsToBounds = true

这在iPhone X上完美运行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM