如何在iOS 9中呈现mfmessagecomposeviewcontroller时更改导航栏的颜色

4
        MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc]init];
        [UINavigationBar appearance].barTintColor = [UIColor colorWithRed:0.0f/255.0f green:127.0f/255.0f blue:254.0f/255.0f alpha:1.0];
        [[messageController navigationBar] setTintColor: [UIColor whiteColor]];
        messageController.messageComposeDelegate = self;
        [messageController setBody:message];
        [messageController navigationBar].translucent =NO;                                             
        [messageController.navigationBar setBarTintColor:[UIColor colorWithRed:0.0f/255.0f green:127.0f/255.0f blue:254.0f/255.0f alpha:1.0]];
        // Present message view controller on screen
        [self presentViewController:messageController animated:YES completion:^{
        [messageController navigationBar].translucent = NO;
        }];

我一直使用这段代码。如果有缺失,请告知我。

请编辑您的帖子以使代码可读。提示:每行缩进4个空格以形成代码块。 - Avi
尝试将设置tintColor的那行代码移动到presentation的完成处理程序中。 - Avi
@Bhumica 它在除了MFMessageComposeViewController之外的每个其他屏幕上更新导航栏颜色和导航栏文本颜色。 - Ali Mumtaz
我已经在初始化 MFMessageComposeViewController 之前调用了这个方法,它已经改变了应用程序中所有导航栏的颜色,但在 MFMessageComposeViewController 中除外。 - Ali Mumtaz
仔细检查你的代码。你做错了什么。在我的应用程序中完全可以运行。 - user3432164
显示剩余7条评论
2个回答

5
您只需要添加两行代码即可更改导航栏颜色。
对于 MFMailComposeViewController。
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];

[mc.navigationBar setTintColor:[UIColor whiteColor]];//cancel button will be of white color.
[mc.navigationBar setBarTintColor:[UIColor blackColor]];//bar color will be black.

if (mc != nil)
        {
            mc.mailComposeDelegate = self;
            [mc setSubject:emailTitle];
            [mc setMessageBody:bodyText isHTML:YES];
            [mc setToRecipients:toRecipents];

            [self presentViewController:mc animated:YES completion:nil];

        }

对于MFMessageComposeViewController

创建以下方法以更改导航栏颜色。

- (void)setNavBarColor:(UIColor *)navBarColor titleColor:(UIColor *)titleColor {
    [[UINavigationBar appearance] setBarTintColor:navBarColor];
    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                          [UIFont fontWithName:@"Futura-Medium" size:17.0f],
                                                          UITextAttributeFont,
                                                          titleColor,
                                                          UITextAttributeTextColor,
                                                          nil]];
}

在初始化MFMessageComposeViewController之前,请编写此方法。(这非常重要,否则它将无法正常工作)
在iOS 9中,这段代码对我很有效。也许它会对你有所帮助。

导航栏中没有显示黑色或任何其他颜色。也尝试过这个。 - Ali Mumtaz
我正在使用MFMessageComposeViewController。已经尝试过了。 - Ali Mumtaz
对我来说也一样,使用MFMessageComposeViewController无法正常工作。 - Avi
验证了它确实适用于MFMailComposeViewController。不知道它们的实现为什么会如此不同,以至于这段代码只适用于其中的一个。 - Avi
对我来说,MFMailComposeViewController无法正常工作。 - Ali Mumtaz
MfMessageCompose...正常工作。不要忘记如上所述:在初始化MFMessageComposeViewController之前编写此方法。(这非常重要,否则它将无法工作)。 - SHS

0

我尝试了上面提到的解决方案进行编辑,但并没有完全成功。不过,在一些帮助下,我找到了一个解决方案。我的解决方案适用于iOS12.1+的构建目标,并在iOS15.4上进行了测试:

MFMessageComposeViewController *messageController = [MFMessageComposeViewController new];
[messageController.navigationBar setBarTintColor: [UIColor redColor]];
[self presentViewController:messageController animated:true completion:nil];
[UIView appearanceWhenContainedInInstancesOfClasses:[NSArray arrayWithObject:[MFMessageComposeViewController class]]].tintColor = [UIColor redColor];

MFMessageComposeViewController: 无法在深色模式下设置“取消”按钮样式


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接