【问题标题】:iOS 10 can no longer set barcolor and tint on MFMessageComposeViewControlleriOS 10 不能再在 MFMessageComposeViewController 上设置 barcolor 和 tint
【发布时间】:2018-08-15 15:59:50
【问题描述】:

以下代码适用于 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 上的应用中遇到了同样的问题;我尝试了很多不同的东西,但没有任何工作。

标签: colors uinavigationbar mfmessagecomposeview


【解决方案1】:

问题

  • 由于 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)
    }
}

希望对你有帮助!

【讨论】:

  • 非常感谢,你拯救了我的一天! ??
  • 谢谢,在使用消息应用分享时,如何影响导航栏的外观真的让我摸不着头脑
【解决方案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 之前,我更改了标题文本属性。例如:

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

【讨论】:

    【解决方案3】:

    这对我来说至少适用于标题和按钮项目。

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

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

    希望这会有所帮助。干杯

    【讨论】:

    • 这是我们尝试过的,但我们最终只能设置两种颜色(文本/背景)中的两种。所以将不得不忍受一个不适合应用程序的窗口。
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      相关资源
      最近更新 更多