ios Swift 和 Xcode - 如何创建自定义标签栏图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30429844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:03:57  来源:igfitidea点击:

Swift and Xcode - How to Create Custom Tab Bar Icons

iosxcodetabsiconsphotoshop

提问by Matt Kelly

I have a tabbed application project I am working on in Xcode written in Swift (Xcode 6.3 and Swift 1.2). I am having a lot of trouble with custom Tab Bar icons. I have designed an image in Photoshop (CS6), saved it as a PNG, resized it in Prepo to be 30x30and imported it into Xcode in the asset library. I then set the tab view controllersicon to that image. However, it doesn't show up.

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

I have looked at these pages but not found any help:
https://www.youtube.com/watch?v=4qqqoAWNfZACustom tab bar icon colorshttp://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

我查看了这些页面,但没有找到任何帮助:
https: //www.youtube.com/watch?v=4qqqoAWNfZA自定义标签栏图标颜色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

What is the proper process to create custom tab bar icons?

创建自定义标签栏图标的正确过程是什么?

回答by Matt Kelly

After a bit of research I resolved the issue, so thought I'd post here in case anyone else has a similar issue. In Photoshop I did the following:

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

  1. Imported the image I wanted to use as the tab bar icon (its easier if you use a black and white image so that you don't have to remove colour).
  2. Set the background to 'Transparent' rather than white.
  3. Removed all white from the image so that it was just a black image with a transparent background.
  4. Saved the image as a .png.
  5. Resized the image to be a square with dimensions 75x75 pixels(and named [email protected]), 50x50 pixels(and named [email protected]), and 25x25 pixels(and named imageName.png)
  1. 导入了我想用作标签栏图标的图像(如果您使用黑白图像则更容易,这样您就不必去除颜色)。
  2. 将背景设置为“透明”而不是白色。
  3. 从图像中删除所有白色,使其只是具有透明背景的黑色图像。
  4. 将图像另存为 .png。
  5. 将图像调整为具有尺寸75x75 pixels(并命名为[email protected])、50x50 pixels(并命名为[email protected])和25x25 pixels(并命名为imageName.png)的正方形

In Xcode I did the following:

在 Xcode 中,我执行了以下操作:

  1. Dragged the images into Xcode and renamed the image group as icoImageName.
  2. Selected the tab I wanted to set the image for in the storyboard in Xcode and set the 'Image' (under 'Bar Item' in the Inspector Pane) to icoImageName. Note that I did not set the 'Selected Image' under the 'Tab Bar Item' (leave this blank).
  1. 将图像拖入 Xcode 并将图像组重命名为icoImageName.
  2. 选择我想在 Xcode 的故事板中设置图像的选项卡,并将“图像”(在检查器窗格中的“栏项”下)设置为icoImageName. 请注意,我没有在“标签栏项目”下设置“选定图像”(将此留空)。

Done.

完毕。

I hope this helps someone. Thanks to everyone for their help as well.

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

回答by MacInnis

It sounds like you have everything set up properly in xCode. The problem IS the png file you are using.

听起来您已经在 xCode 中正确设置了所有内容。问题是您正在使用的 png 文件。

Download this image, http://i.stack.imgur.com/zluev.png, and see if the problem persists.

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

According to an answer on UITabBarItem images just appear as a grey block:

根据对UITabBarItem 图像的回答仅显示为灰色块

The standard tabbar icons in iOS are rendered solely from the alpha channel. Colors are ignored completely. Instead of colors you can use different alpha values that lead to a different shade of gray (or blue if selected)

Make the background of your icons transparent.

iOS 中的标准标签栏图标仅从 Alpha 通道呈现。颜色被完全忽略。您可以使用不同的 alpha 值来代替颜色,从而产生不同的灰色阴影(或蓝色,如果选择)

使图标的背景透明。

回答by Luke

Did you create the tab view in interface builder? If so, since you added the images as an asset they should show up in the 'Image' property of each tab button under the inspector sidebar. Also, I know you've already posted a ton of tutorials, but this one is pretty up to date and explains it thoroughly: http://codewithchris.com/ios-tab-bar-app/

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

回答by Nikunj Patel

enter image description here

在此处输入图片说明

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


}