Swift和Xcode – 如何创build自定义标签栏图标

我在用Swift编写的Xcode 6.3 and Swift 1.2Xcode 6.3 and Swift 1.2 )中有一个选项卡式的应用程序项目。 我在自定义选项卡栏图标上遇到了很多麻烦。 我在Photoshop(CS6)中devise了一个图像,将其保存为PNG,将其大小调整为30x30并将其导入到资源库的Xcode中。 然后,我将tab view controllers图标设置为该图像。 但是,它并没有显示出来。

我看过这些页面,但没有find任何帮助:
https://www.youtube.com/watch?v=4qqqAWAWF 自定义选项卡栏图标颜色 http://www.raywenderlich.com/forums/viewtopic.php?f=2&t=19333
http://www.appcoda.com/ios-programming-how-to-customize-tab-bar-background-appearance/ https://www.youtube.com/watch?v=4Tj_SeApUrs

什么是适当的过程来创build自定义标签栏图标?

经过一些研究,我解决了这个问题,所以我想在这里发帖,以防其他人有类似的问题。 在Photoshop中,我做了以下几点:

  1. 导入我想用作标签栏图标的图像(如果您使用黑白图像,则更容易,因此不必去除颜色)。
  2. 将背景设置为“透明”而不是白色。
  3. 删除图像中的所有白色,以便它只是一个背景透明的黑色图像。
  4. 将图像保存为.png。
  5. 将图像调整为尺寸为75x75 pixels (并命名为imageName@3x.png ), 50x50 pixels (并命名为imageName@2x.png )和25x25 pixels (并命名为imageName.png )的正方形。

在Xcode中我做了以下几点:

  1. 将图像拖到Xcode中,并将图像组重命名为icoImageName
  2. select我想在Xcode的故事板中设置图像的选项卡,并将“图像”(Inspector Pane中的“Bar Item”)设置为icoImageName 。 请注意,我没有在“选项栏项目”下面设置“选定的图像”(请留空)。

完成。

我希望这可以帮助别人。 感谢大家的帮助。

这听起来像你已经在xCode中正确设置了一切。 问题是你正在使用的PNG文件。

下载这个图片, http://i.stack.imgur.com/zluev.png ,看看问题是否存在。

根据UITabBarItem上的答案, 图像只是灰色块 :

iOS中的标准标签栏图标仅从Alpha通道渲染。 颜色被完全忽略。 您可以使用不同的阿尔法值来导致不同的灰色阴影(或者select蓝色)

使您的图标背景透明。

您是否在界面构build器中创build了标签视图? 如果是这样,由于您将图片添加为资源,它们应显示在检查器侧边栏下的每个选项卡button的“图像”属性中。 另外,我知道你已经发布了大量的教程,但是这个教程是相当新的,并且是彻底的解释: http : //codewithchris.com/ios-tab-bar-app/

在这里输入图像说明

 class ViewController: UIViewController { @IBOutlet var btnHome : UIButton! @IBOutlet var btnInvoice : UIButton! @IBOutlet var btnSettings : UIButton! @IBOutlet var btnMyOrder : UIButton! @IBOutlet var btnLogout : UIButton! @IBOutlet weak var viewContainer: UIView! var navController : UINavigationController! var selectedIndex : Int! = 0 var arrTabColor = [UIColor(red: 35.0/255.0, green: 93.0/255.0, blue: 175.0/255.0, alpha: 1.0), UIColor(red: 29.0/255.0, green: 86.0/255.0, blue: 167.0/255.0, alpha: 1.0), UIColor(red: 35.0/255.0, green: 93.0/255.0, blue: 175.0/255.0, alpha: 1.0), UIColor(red: 29.0/255.0, green: 86.0/255.0, blue: 167.0/255.0, alpha: 1.0), UIColor(red: 35.0/255.0, green: 93.0/255.0, blue: 175.0/255.0, alpha: 1.0)] var arrTabIdentiFierVC = ["FirstVC","SecondVC","FirstVC","FirstVC","SecondVC"] // MARK: - Life Cycle override func viewDidLoad() { super.viewDidLoad() setTabbarImage(0) // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func setTabBarClicked(_ storyIdentifier : String,identifier : String) { let aStoryboard = UIStoryboard.init(name: storyIdentifier, bundle: nil) let newViewController = aStoryboard.instantiateViewController(withIdentifier: identifier) navController = UINavigationController(rootViewController: newViewController) self.addChildViewController(navController) navController.view.frame = viewContainer.frame newViewController.view.frame = viewContainer.frame self.viewContainer.addSubview(navController.view) newViewController.didMove(toParentViewController: self) } func setTabbarImage(_ selectedIndex : Int!) { btnHome.backgroundColor = arrTabColor[0] btnInvoice.backgroundColor = arrTabColor[1] btnSettings.backgroundColor = arrTabColor[2] btnMyOrder.backgroundColor = arrTabColor[3] btnLogout.backgroundColor = arrTabColor[4] let selectedColor = UIColor(red: 40/255, green: 142/255, blue: 206.0/255, alpha: 1.0) if selectedIndex == 0 { btnHome.backgroundColor = selectedColor } else if selectedIndex == 1 { btnInvoice.backgroundColor = selectedColor } else if selectedIndex == 2 { btnSettings.backgroundColor = selectedColor } else if selectedIndex == 3 { btnMyOrder.backgroundColor = selectedColor } else if selectedIndex == 4 { btnLogout.backgroundColor = selectedColor } } // MARK: - Action Method @IBAction func HomeClicked(_ sender : AnyObject?) { setTabbarImage(0) setTabBarClicked("Main",identifier: arrTabIdentiFierVC[0]) } @IBAction func InvoiceClicked(_ sender : AnyObject?) { setTabbarImage(1) setTabBarClicked("Main",identifier: arrTabIdentiFierVC[1]) } @IBAction func SettingClicked(_ sender : AnyObject?) { setTabbarImage(2) setTabBarClicked("Main",identifier: arrTabIdentiFierVC[2]) } @IBAction func MyorderClicked(_ sender : AnyObject?) { setTabbarImage(3) setTabBarClicked("Main",identifier: arrTabIdentiFierVC[3]) } @IBAction func logoutClicked(_ sender : AnyObject?) { setTabbarImage(4) let alert = UIAlertController(title: "", message: "Are you sure want to logout?", preferredStyle: UIAlertControllerStyle.alert) let CancelAction = UIAlertAction(title: "NO", style: .default) { (action:UIAlertAction!) in } alert.addAction(CancelAction) let OKAction = UIAlertAction(title: "YES", style: .default) { (action:UIAlertAction!) in // var isNav : Bool! = false //for objChild in (self.parent?.childViewControllers)! // { // if objChild.isKind(of: LoginVC.self) // { // self.navigationController!.popToViewController(objChild, animated: true) // CommonMethods.removeCustomObject(Constants.kUserProfile) // // isNav = true // break // // } // } // if !isNav // { // CommonMethods.removeCustomObject(Constants.kUserProfile) // let aNavController = (AppDelegate.getDelegate().window!.rootViewController! as! UINavigationController) // let storyboard = UIStoryboard(name: "Main", bundle: nil) // var aVCObj = UIViewController() // aVCObj = storyboard.instantiateViewController(withIdentifier: "LoginVC") // var aMutArr = aNavController.viewControllers // aMutArr.insert(aVCObj, at: 0) // aNavController.viewControllers = aMutArr // aNavController.popToRootViewController(animated: true) // } } alert.addAction(OKAction) self.present(alert, animated: true, completion: nil) } // MARK: - Action Method }