iOS 10 无法再在 MFMessageComposeViewController 上设置条形颜色和色调

iOS 10 can no longer set barcolor and tint on MFMessageComposeViewController

下面的代码适用于 iOS 版本 9.x 或更低版本,由于某些原因,如果 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];
             }

它是坏了还是做了一些改变。不确定这里缺少什么。我在黑暗中(漆黑)

编辑: 我试图在一个新的空单视图项目上使用一些测试代码,但我遇到了同样的问题。

@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)
    } 

编辑: UINavigationBar 外观可以在测试应用程序中为背景或 barTint 设置颜色,但我仍然无法为测试应用程序设置文本颜色。我正在使用的应用程序已经使用 UINavigationBar 外观来设置整个应用程序的导航栏颜色,但这不会影响 SMS 的导航栏,因为它会出现白色背景和白色文本。无法更改文本颜色或背景颜色使该视图不可用。

在 iOS 10 之前,我使用的是 UINavigationBar 的外观代理,如下所示:

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];

这涵盖了我对 MFMessageComposeViewController 的使用,setTitleTextAttributes 负责 title/caption 的文本颜色。

对于 iOS 10,我正在使用以下解决方法。在显示 MFMessageComposeViewController 之前,我更改了 标题文本属性 。例如:

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

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

    [[UINavigationBar appearance] setTitleTextAttributes:newTitleAttribs];

    // present the MFMessageComposeViewController...
}

然后在 MFMessageComposeViewController 完成后,将应用程序的其余部分设置回我想要的方式,例如:

- (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...
    }];
}

这至少适用于标题和按钮项目。

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

在此处查找更多信息:https://developer.apple.com/reference/uikit/uiappearance

希望这对您有所帮助。干杯

问题

  • 出于某些原因 iOS10.x barTintColor 无法在某些共享容器上工作。
  • 但是有一个解决方法可以修复所有共享容器上的导航栏颜色。

解决方案

  • 使用UINavigationBar.appearance()更改导航栏颜色。
  • 使用backgroundColor 属性 & setBackgroundImage(_:for:) 方法修复导航栏颜色。

代码

/// 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)
    }
}

希望对您有所帮助!

Objective-C解法:

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

使用颜色方法的图像:

+ (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;
}