【问题标题】:Swift change color of NavigationControl and TabBar快速更改 NavigationControl 和 TabBar 的颜色
【发布时间】:2015-01-20 19:37:13
【问题描述】:

我想改变所有应用的 NavigationControl 的后退按钮的颜色,我该怎么做?我想用红色而不是普通的蓝色按钮...

我的 TabBar 有问题。 我已将图标颜色和名称从标准蓝色更改为红色:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected)

还有这个

class TabBarController: UITabBarController {

var color = UIColor.redColor()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.tabBarController?.tabBar.selectedImageTintColor = UIColor.redColor()
    UITabBar.appearance().selectedImageTintColor = UIColor.redColor()

但是我有超过5个标签,所以我有“更多”按钮,当我按下它时,图标不是红色而是蓝色,当我按下编辑标签栏图标时,名称选项卡是红色的,但图标不是。 我能做些什么? 图片说明: http://postimg.org/image/67oqa15ll/

【问题讨论】:

    标签: swift uinavigationcontroller uitabbarcontroller tabbar


    【解决方案1】:

    更改整个应用程序中所有 UITabBarItems 的文本和图标颜色(在 Swift / Xcode 6 中):

    在 AppDelegate.swift 中:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
        // Override point for customization after application launch.
    
        UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: your_color ], forState: .Selected)
    
        UITabBar.appearance().tintColor = your_color
    
        return true
    
    }
    

    这样就行了!

    【讨论】:

    • 它在tabBar中工作,但在“更多”部分,其他图标仍然是蓝色的......
    • 在 Swift 3.0 中代替 forState: 使用 for:。 UIControlState.Selected 也稍微改成小写:UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: your_color], for: .selected)
    【解决方案2】:

    在你的 firstViewController 中试试这个

    class ViewController: UIViewController,UITabBarDelegate, UITabBarControllerDelegate, UINavigationControllerDelegate {
    
            override func viewDidLoad() {
                super.viewDidLoad()
    
                UITabBar.appearance().tintColor = UIColor.redColor()
                var view: UITableView = self.tabBarController?.moreNavigationController.topViewController.view as UITableView
                view.tintColor = UIColor.redColor()
                if view.subviews.count != 0 {
                    for cell in view.visibleCells() {
                        cell.textLabel??.textColor = UIColor.redColor()
                    }
                }
            }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    }
    

    这在你的 UITabbarController 中

    class TabbarViewController: UITabBarController,UITabBarDelegate, UITabBarControllerDelegate, UINavigationControllerDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        override func tabBar(tabBar: UITabBar, didBeginCustomizingItems items: [AnyObject]) {
            self.view.tintColor = UIColor.redColor()
    
        }
    }
    

    示例项目https://www.dropbox.com/s/kbm4l60cnvyrf5h/UItabbarCustomizing.zip?dl=0

    希望对你有帮助

    【讨论】:

    • 您好,谢谢您的回答。你用 NavigationController 解决了我的问题,但我也遇到了 TabBar 的问题。在“更多”中,如何将图标的颜色从蓝色更改为红色?我怎样才能让它在“编辑”页面里面也能工作?
    • 谢谢,它可以在 tabBar 上工作,但是“更多”选项卡有一个错误。当我按下它时,它会打开一个带有我的其他标签的表格,带有他们的图标,但问题是图标不是红色的,但这些图标仍然是蓝色的......如何修复它?
    • 那是我的问题:postimg.org/image/bcw1q66cp 颜色在 more 中不起作用,但仅在 tabBar 中起作用,而不是文本起作用。我想要所有的图标都是红色而不是蓝色。
    • 我按你说的试过了,但还是不行,我也不知道怎么把我的项目上传到这里……我在AppDelegate里有这个,但如果我把它也放进去任何viewController,没有任何改变... UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected) UITabBar.appearance().selectedImageTintColor = UIColor.redColor()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 2019-11-28
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多