无法在UITabBarItem的外观上读取titleTextAttributes

时间:2015-09-02 21:59:00

标签: ios swift uikit uiappearance

我正致力于构建我正在构建的应用程序。我想将一些系统范围的样式(字体)应用于选项卡栏,然后在某些实例中应用颜色等样式。我遇到了两个问题:

  1. 当您在UITabBar的实例上使用setTitleTextAttributes:forState:设置任何titleTextAttributes时,它会立即忽略在外观代理上设置的任何titleTextAttributes(包括未在实例上设置但在外观上设置的键代理)。

    // AppDelegate
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        UITabBarItem.appearance().setTitleTextAttributes([
            NSFontAttributeName: UIFont(name: "Lato-Regular", size: 20.0)!
            ], forState: .Normal)
    }
    
    // Later on in a ViewController that has a UITabBar
    override func viewDidLoad() {
        myTabBar.setTitleTextAttributes([
            NSForegroundColorAttributeName: UIColor.blueColor()
            ], forState: .Normal)
    
        // Tab bar items now have blue text, but Helvetica Neue font
        // We've lost the appearance proxy font (Lato-Regular)
    }
    
  2. 为了修复(1)我只是从外观代理复制titleTextAttributes,然后用我想在实例上应用的任何属性覆盖它们。例如。

    // AppDelegate
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        UITabBarItem.appearance().setTitleTextAttributes([
            NSFontAttributeName: UIFont(name: "Lato-Regular", size: 20.0)!
            ], forState: .Normal)
    }
    
    // Later on in a ViewController that has a UITabBar
    override func viewDidLoad() {
        var attributes = UITabBarItem.appearance().titleTextAttributesForState(state) ?? [NSObject: AnyObject]()
    
        assert(attributes.count > 0, "Could not read titleTextAttributes set on appearance proxy!")
    }
    
  3. 这是最烦人的,因为你可以在UINavigationBar上读取外观代理值就好了。

    有什么想法吗?

0 个答案:

没有答案