jeudi 13 août 2015

how to handle ContentSizeCategoryDidChangeNotification more elegant

I have a navigation bar, and set its title with preferredFontForTextStyle, And register for UIContentSizeCategoryDidChangeNotification, but every time, when deal with this notification, I have to reset the titleTextAttributes of the navigation bar to get the title bar updated when I change the Lager Text in accessibility in settings, I just want to know if there is a elegant way to update the title, the code is just below:

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.navigationBar.titleTextAttributes = [
        NSFontAttributeName: UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline),
        NSForegroundColorAttributeName: List.Color.Gray.colorValue
        ]

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleContentSizeCategoryDidChangeNotification:", name: UIContentSizeCategoryDidChangeNotification, object: nil)
   }

    // MARK: Lifetime

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIContentSizeCategoryDidChangeNotification, object: nil)
    }

    // MARK: Notifications

    func handleContentSizeCategoryDidChangeNotification(_: NSNotification) {
        navigationController?.navigationBar.titleTextAttributes = [
        NSFontAttributeName: UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline),
        NSForegroundColorAttributeName: List.Color.Gray.colorValue
        ]
    }

PS. I tried to call setNeedsLayout() and call layoutIfNeeded() of the navigation bar, but it doesn't update the title font, the upper method is fine, but i want a more elegant solution, like call a method from apple.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire