2017-07-26 280 views
0

我只想更改其中一個選項卡bat項目的背景顏色。我發現很多鏈接,但沒有得到任何幫助。更改Swift中UITabBarItem的背景顏色

要求: enter image description here

而且,這是我的方式設置我的標籤欄項目

let myTabBarItem3 = (self.tabBar.items?[2])! as UITabBarItem 
myTabBarItem3.image = UIImage(named: "ic_center")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
myTabBarItem3.selectedImage = UIImage(named: "ic_center")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) 

我要的是對中心標籤欄項目的黑色背景。

有什麼想法?

是的它是不是重複,因爲回答了前面的不準確,並增加額外的子視圖從來都不是一個好的選擇,所以希望從朋友一些好的解決方案

回答

0

您可以將子視圖添加到parent tabBar 然後,您可以在子視圖上設置背景顏色。 計算tabBarItem的偏移量和寬度,並在其下插入subView。

let itemIndex = 2 
let bgColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1.0) 

let itemWidth = tabBar.frame.width/CGFloat(tabBar.items!.count) 
let bgView = UIView(frame: CGRectMake(itemWidth * itemIndex, 0, itemWidth, tabBar.frame.height)) 
bgView.backgroundColor = bgColor 
tabBar.insertSubview(bgView, atIndex: 0) 
+0

謝謝你..我已經通過這個答案在這裏:https:// stacko verflow.com/a/26753432/4033273 –

+0

還有其他更好的選擇嗎? –

0

可以使用的ImageView來實現這一影響,如果你只想改變中心tabBarItem的背景顏色,你可以按照下面的代碼試試這個辦法

let myImageView: UIImageView = { 
     let imageView = UIImageView() 
     return imageView 
    }() 
    //Now add this imageView as subview and apply constraints 
    tabbar.addSubview(myImageView) 
    myImageView.translatesAutoresizingMaskIntoConstraints = false   
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[v0(28)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": myImageView])) 
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[v0(28)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": myImageView])) 



    tabbar.myImageView.image = UIImage(named: "ic_center")?.withRenderingMode(UIImageRenderingMode.alwaysTemplate) 
    tabbar.myImageView.tintColor = UIColor.black 
+0

什麼是addconstraints這裏? –

+0

這個方法會在視圖上添加約束, – 3stud1ant3

+0

那我知道但是這裏有什麼用 –

0

注:根據您的用戶界面和中心tabBarItem圖像默認顏色爲白色

class tabbarVCViewController: UITabBarController, UITabBarControllerDelegate { 

    // MARK: - ViewController Override Methods. 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 

     setupInitilView() 
    } 

    // MARK: - setup Initial View Methode. 
    private func setupInitilView() { 

     delegate = self 

     // Sets the default color of the icon of the selected UITabBarItem and Title 
     UITabBar.appearance().tintColor = UIColor.white 

     // Sets the default color of the background of the UITabBar 
     UITabBar.appearance().barTintColor = UIColor.white 

     // Sets the background color of the selected UITabBarItem (using and plain colored UIImage with the width = 1/5 of the tabBar (if you have 5 items) and the height of the tabBar) 
     //UITabBar.appearance().selectionIndicatorImage = UIImage().makeImageWithColorAndSize(color: UIColor.black, size: CGSize.init(width: tabBar.frame.width/4, height: tabBar.frame.height)) 


     // Uses the original colors for your images, so they aren't not rendered as grey automatically. 
     for item in self.tabBar.items! { 
      if let image = item.image { 

       //item.image = image.withRenderingMode(.alwaysTemplate) 

       item.image = image.withRenderingMode(.alwaysOriginal) //Use default image colour as grey colour and your centre image default colour as white colour as your requirement. 
      } 
     } 

     //Change the backgound colour of specific tabBarItem. 

     let itemIndex:CGFloat = 2.0 

     let bgColor = UIColor.black 
     let itemWidth = tabBar.frame.width/CGFloat(tabBar.items!.count) 
     let bgView = UIView(frame: CGRect.init(x: itemWidth * itemIndex, y: 0, width: itemWidth, height: tabBar.frame.height)) 
     bgView.backgroundColor = bgColor 
     tabBar.insertSubview(bgView, at: 0) 

    } 

    // MARK: - UITabbarController Override Methods . 
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { 

    } 


    // MARK: - UITabBarControllerDelegate Methods 
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { 

     return true 
    } 
} 

使用tabBarItem圖像默認顏色爲灰色:下面所有的代碼在一個延伸的UITabBarController作爲一個自定義類使用資產中。

你將要擴展的UIImage類,以與大小素色圖像,你需要:

extension UIImage { 
    func makeImageWithColorAndSize(color: UIColor, size: CGSize) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(size, false, 0) 
     color.setFill() 
     UIRectFill(CGRect.init(x: 0, y: 0, width: size.width, height: size.height)) 
     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image! 
    } 
} 
+0

謝謝你..我已經通過這個答案在這裏:https://stackoverflow.com/a/31680820/4033273。這個答案適用於選中的標籤欄 –

+0

所以這種方法不適合你,如果不是,你可以在上面的代碼中使用代碼 –

+0

@Moin Shirazi檢查更新後的答案。 –

0

試試這個:

UITabBar.appearance().tintColor = UIColor.pink 
UITabBar.appearance().barTintColor = UIColor.white 
      if #available(iOS 10.0, *) { 
       UITabBar.appearance().unselectedItemTintColor = UIColor.white 
      } else { 
       // Fallback on earlier versions 
      } 
let x = Double(UIScreen.main.bounds.width/5.0) 
let y = Double(tabBarController!.tabBar.frame.size.height) 
let indicatorBackground: UIImage? = self.image(from: UIColor.black, for: CGSize(width: x, height: y)) 
UITabBar.appearance().selectionIndicatorImage = indicatorBackground 

輔助方法

func image(from color: UIColor, for size: CGSize) -> UIImage { 
    let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) 
    autoreleasepool { 
     UIGraphicsBeginImageContext(rect.size) 
    } 
    let context: CGContext? = UIGraphicsGetCurrentContext() 
    context?.setFillColor(color.cgColor) 
    context?.fill(rect) 
    let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return image! 
}