Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITabBar背景图像的图像缩放_Ios_Swift_Background_Uitabbar - Fatal编程技术网

Ios UITabBar背景图像的图像缩放

Ios UITabBar背景图像的图像缩放,ios,swift,background,uitabbar,Ios,Swift,Background,Uitabbar,我已经在我的应用程序中创建了UITabBarController 然后在viewDidLoad()中,我想更改UITabBar背景图像。以下是我试图使其工作的代码: class MainTabBarController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() UITabBar.appearance().translucent = false

我已经在我的应用程序中创建了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
    }
}

但结果并不正确()。有人能帮我让它填满整个选项卡栏框架吗?

尝试将
图像调整为选项卡栏大小。或者将
imageView
作为
子视图添加到
选项卡
,然后使用该
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)
}

这在swift 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上非常有效

当将图像视图放置到选项卡栏时,它会显示在选项卡栏按钮上方,而看不到任何按钮。在选项卡栏项上使用view.bringSubviewToFront()使它们可见
clipstobunds
帮助我剪辑图像。