iOS 10无法再在MFMessageComposeViewController上设置barcolor和色调

时间:2016-09-12 15:31:55

标签: colors uinavigationbar mfmessagecomposeview

以下代码适用于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的导航栏,因为它出现白色背景和白色文本。无法更改文本颜色或背景颜色使此视图无法使用。

4 个答案:

答案 0 :(得分:11)

<强>问题

  • 由于iOS10.x中的某些原因, barTintColor 无法在某些共享容器上运行。
  • 但是有一种解决方法可以修复所有共享容器上的导航栏颜色。

<强>解决方案

  • 使用UINavigationBar.appearance()更改导航栏颜色。
  • 使用backgroundColor属性&amp;修复导航栏颜色的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)
    }
}

希望它有所帮助!

答案 1 :(得分:2)

在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负责标题/标题的文字颜色。

使用iOS 10,我正在使用以下方法。就在我出示MFMessageComposeViewController之前,我更改了标题文字属性。 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...
}

然后在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...
    }];
}

答案 2 :(得分:1)

至少对于标题和按钮项目,这对我有用。

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

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

希望这会有所帮助。干杯

答案 3 :(得分:0)

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