Ios UITabBar背景图像的图像缩放
我已经在我的应用程序中创建了UITabBarController 然后在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
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
帮助我剪辑图像。