Custom Font in NavigationBar of iOS9 MFMailComposeViewController?
Asked Answered
J

4

10

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

Juratory answered 30/6, 2016 at 16:43 Comment(0)
N
3

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.

Normalie answered 11/7, 2016 at 4:7 Comment(0)
V
0

Inside the old documentation it was clearly stated here that you MUST NOT change the interface provided by Apple.

http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html

Important: The mail composition interface itself is not customizable and must not be modified by your application. In addition, after presenting the interface, your application is not allowed to make further changes to the email content. The user may still edit the content using the interface, but programmatic changes are ignored. Thus, you must set the values of content fields before presenting the interface.

Vexation answered 5/7, 2016 at 12:43 Comment(3)
They state in the same link you provided that : Important You must not modify the view hierarchy presented by this view controller. You can, however, customize the appearance of the interface using the UIAppearance protocol.Juratory
It seems imposible to change the title because it turns back. I tried to change it directly with mailController.navigationBar.titleTextAttributes = ... but the effect is the same, it turns back to default. Even if I try to change it after some time delay it cant be changed. Sword in stone :)Marasmus
That's the reason of this question and it's bounty, to find out why and if there's some way to do it. I already tried all of this and several combinations. It's the same for me. It turns back to default :(Juratory
I
0

Apple's framework probably uses the appearance apis itself, have you tried a more specific appearance 'selector' eg:

UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([MFMailComposeViewController.self, UINavigationBar.self])
Infantile answered 8/7, 2016 at 8:4 Comment(0)
S
-1

UIFont() is a failable initializer, it may fail for many reasons.

Some people say not to force unwrap and to initialize it separately and check for success:

`if let font = UIFont(name: "customFont.ttf", size: 21) {
UINavigationBar.appearance().titleTextAttributes =  [NSFontAttributeName: font]
}`

Have you tried initializing it separately?

Slipover answered 4/7, 2016 at 23:14 Comment(1)
If it fails to load the font it will crash. This is not the case hereJuratory

© 2022 - 2024 — McMap. All rights reserved.