iOS 10 can no longer set barcolor and tint on MFMessageComposeViewController
Asked Answered
B

4

21

Below code works on iOS version 9.x or less, for some reason this does not work if iOS 10

 if([MFMessageComposeViewController canSendText])
             {
                 controller.body = message;
                 NSString *tel = pContact.tlc;
                 controller.recipients = pContact.tlc?@[tel]:nil;
                 controller.messageComposeDelegate = self;
                 controller.navigationBar.tintColor = [UIColor whiteColor];
                 controller.navigationBar.barTintColor = [UIColor blueColor];
                 [self presentViewController:controller animated:YES completion:nil];
             }

is it broken or did some thing change. Not sure whats missing here. I am in the dark (pitch black)

EDIT: I tried to use some test code on a new empty single view project and I am getting the same problems.

@IBAction func SMS(_ sender: AnyObject) {
        let composeVC = MFMessageComposeViewController()
        composeVC.messageComposeDelegate = self

        // Configure the fields of the interface.
        composeVC.recipients = ["5555555555"]
        composeVC.body = "Hello from California!"
        composeVC.navigationBar.tintColor = UIColor.green
        composeVC.navigationBar.barTintColor = UIColor.purple
        // Present the view controller modally.
        self.present(composeVC, animated: true, completion: nil)
    } 

Edit: UINavigationBar appearance can set the color in a test App for the background or barTint but I am still unable to set the text color for the test app. The app I am working on uses the UINavigationBar appearance already to set the navbar color across the app, but this is not affecting the navbar for the SMS as it come up white background and white text. not able to change the text color or background color make this view un-usable.

Balcom answered 12/9, 2016 at 15:31 Comment(1)
I'm having the same problem in my app on iOS 10; I've tried many different things and haven't gotten anything to work. – Selfsatisfied
T
11

Issue

  • For some reasons in iOS10.x barTintColor is not working on some of the sharing containers.
  • But there's a workaround to fix the Navigation bar colour on all the sharing containers.

Solution

  • Use UINavigationBar.appearance() to change the navigation bar colour.
  • Use backgroundColor property & setBackgroundImage(_:for:) method to fix the navigation bar colour.

Code

/// Method to set navigation bar color
func setNavigationBar() -> Void
{
    // barTintColor will apply color for the app's navigation bar
    UINavigationBar.appearance().barTintColor       = UIColor.blue

    // backgroundColor will apply color for some of the sharing container's app except for Messages app
    UINavigationBar.appearance().backgroundColor    = UIColor.blue

    // backgroundImage will apply the color image for navigation bar for most of the sharing container's except for `Notes`
    UINavigationBar.appearance().setBackgroundImage(UIImage(color: UIColor.blue), for:UIBarMetrics.default)
}

/// UIImage extension to create an image from specified color
extension UIImage
{
    public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
        let rect = CGRect(origin: .zero, size: size)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        color.setFill()
        UIRectFill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        guard let cgImage = image?.cgImage else { return nil }
        self.init(cgImage: cgImage)
    }
}

Hope it helps!.

Tipster answered 8/4, 2017 at 2:58 Comment(2)
Thank you very much, you saved my day! πŸ™‚πŸ‘ – Bacteriology
Thanks, was really scratching my head as to how to affect the appearance of the nav bar when sharing using the messages app – Intinction
U
2

Prior to iOS 10, I was using the appearance proxy for UINavigationBar, something like this:

NSDictionary* titleAttribs = @{ NSForegroundColorAttributeName: [ColoursAndStyles sharedInstance].fontColourNavBarTitle,
                                NSFontAttributeName: [ColoursAndStyles sharedInstance].fontNavbarBoldTitle };

[[UINavigationBar appearance] setBarTintColor:[ColoursAndStyles sharedInstance].viewColourNavBarMain];
[[UINavigationBar appearance] setTintColor:[ColoursAndStyles sharedInstance].viewColourNavBarTint];
[[UINavigationBar appearance] setTitleTextAttributes:titleAttribs];

This covered my use of MFMessageComposeViewController, with setTitleTextAttributes taking care of the text colour of the title/caption.

With iOS 10, I'm using the following work around. Just before I present the MFMessageComposeViewController, I change the title text attributes. E.g.:

- (void)sendTap:(id)s
{
    // some code...

    NSDictionary* newTitleAttribs = @{ NSForegroundColorAttributeName: [ColoursAndStyles sharedInstance].fontColourTitleStrip,
                                       NSFontAttributeName: [ColoursAndStyles sharedInstance].fontNavbarBoldTitle };

    [[UINavigationBar appearance] setTitleTextAttributes:newTitleAttribs];

    // present the MFMessageComposeViewController...
}

And then set things back to how I want them for the rest of the app, when the MFMessageComposeViewController finishes, e.g.:

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    NSDictionary* titleAttribs = @{ NSForegroundColorAttributeName: [ColoursAndStyles sharedInstance].fontColourNavBarTitle,
                                    NSFontAttributeName: [ColoursAndStyles sharedInstance].fontNavbarBoldTitle };

    [[UINavigationBar appearance] setTitleTextAttributes:titleAttribs];

    // some code...

    [controller dismissViewControllerAnimated:YES completion:^{
        // some code...
    }];
}
Unionism answered 20/10, 2016 at 22:35 Comment(0)
M
1

This works for me at least for title and button items.

[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil].tintColor = [UIColor redColor];
[UINavigationBar appearance].titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor redColor]};

Look for more info here: https://developer.apple.com/reference/uikit/uiappearance

Hope this helps. Cheers

Minardi answered 15/12, 2016 at 16:58 Comment(1)
that is what we tried, but we ended up only being able to set 2 of the two colours (text/background). so will have to live with a window that does not really fit the app. – Balcom
B
0

Objective-C solution:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTranslucent:NO];
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];

Image with color method:

+ (UIImage *)imageWithColor:(UIColor *)paramColor
{
    CGRect frame = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContextWithOptions(frame.size, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);

    CGContextSetFillColorWithColor(context, [paramColor CGColor]);
    CGContextFillRect(context, frame);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
Briard answered 15/8, 2018 at 7:59 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.