Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Font in NavigationBar of iOS9 MFMailComposeViewController?

Alright, this is what I have so far:

enter image description here

As you can notice here I managed to change font size so this is fine, but the style I want also includes a custom font.

Note that the actual style is shown for a moment and then as the statusbar changes to black font the custom font gets losts.

Here's the code I use in my applicationDidFinish...

UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent

UINavigationBar.appearance().titleTextAttributes = [
     NSFontAttributeName: UIFont(name: "<MyCustomFont>", size: 32)!,
     NSForegroundColorAttributeName : UIColor.whiteColor(),
    ]

UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().opaque = true
UINavigationBar.appearance().barStyle = UIBarStyle.Black
UINavigationBar.appearance().barTintColor = UIColor.BlueColor()

UIBarButtonItem.appearance().tintColor = UIColor.whiteColor()

UIBarButtonItem.appearance().setTitleTextAttributes([
     NSFontAttributeName: UIFont(name: "<MyCustomFont>", size: 18)!,
     NSForegroundColorAttributeName : UIColor.whiteColor(),
    ], forState: UIControlState.Normal)

Note:

I have an instance of EKEventEditViewController in place and the style is applied correctly.

The issue appears to be MailComposer's related

like image 848
Hugo Alonso Avatar asked Jun 30 '16 16:06

Hugo Alonso


1 Answers

There's nothing wrong with the code. This is totally justified when it works for instances of EKEventEditViewController and it doesn't work for instances of MFMailComposeViewController

How to investigate this further?

  • Maybe try to run on different versions of iOS. If your application supports iOS8 or lower, try to see if it works there. That way you can make sure it's a iOS9 specific problem or not.

  • If you have Xcode8 beta, you should be able to see if this is fixed in iOS10 or not.

Problem is still there on iOS9. In this case, you need to look for alternatives here. You might have inspected UINavigationBar API completely, I would recommend having a look at UINavigationItem API instead. It exposes a titleView property leaving it up to you how you want to make it look like.

var titleView: UIView?

If this property value is nil, the navigation item’s title is displayed in the center of the navigation bar when the receiver is the top item. If you set this property to a custom title, it is displayed instead of the title.

Custom views can contain buttons. Use the buttonWithType: method in UIButton class to add buttons to your custom view in the style of the navigation bar. Custom title views are centered on the navigation bar and may be resized to fit.

The default value is nil.

A typical use case-

self.navigationItem.titleView = myCustomTitleView

Advantage :-

Opens more possibilities. Other than limited customisation end points provided for UINavigationBar

I would need you to verify if this works. I haven't tried it yet.

Hope it helps.

like image 70
Tarun Tyagi Avatar answered Oct 13 '22 01:10

Tarun Tyagi