更改 MFMailComposeViewController 的标题
我在我的应用程序中使用 MFMailComposeViewController 作为应用内电子邮件,但我无法更改标题。默认情况下,它在标题中显示主题,但我想将标题设置为其他内容。我怎样才能做到这一点?
我已经尝试过:
controller.title = @"Feedback";
但没有成功。
这是我的代码:
- (IBAction)email {
NSArray *array = [[NSArray alloc] initWithObjects:@"[email protected]", nil];
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
[[controller navigationBar] setTintColor:[UIColor colorWithRed:0.36 green:0.09 blue:0.39 alpha:1.00]];
controller.mailComposeDelegate = self;
controller.title = @"Feedback";
[controller setSubject:@"Long subject"];
[controller setMessageBody:@""
isHTML:NO];
[controller setToRecipients:array];
[self presentModalViewController:controller animated:YES];
[controller release];
[array release];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}
I'm using MFMailComposeViewController for in-app email in my app, but I'm not able to change the title. As default it's showing the subject in the title, but I would like to set the title to be something else. How can I do that?
I've tried:
controller.title = @"Feedback";
but it didn't work.
Here's my code:
- (IBAction)email {
NSArray *array = [[NSArray alloc] initWithObjects:@"[email protected]", nil];
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
[[controller navigationBar] setTintColor:[UIColor colorWithRed:0.36 green:0.09 blue:0.39 alpha:1.00]];
controller.mailComposeDelegate = self;
controller.title = @"Feedback";
[controller setSubject:@"Long subject"];
[controller setMessageBody:@""
isHTML:NO];
[controller setToRecipients:array];
[self presentModalViewController:controller animated:YES];
[controller release];
[array release];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用一行为 MFMailComposeViewController 设置不同的标题,如下所示。
但是,此实现实际上依赖于 MFMailComposeViewController 的未记录功能。您正在访问私有类 (_MFMailComposeRootViewController) 的 navigationItem 并将其标题更改为邮件主题以外的其他内容。我赞同 Art Gillespie 的观点,即您不应该这样做,并且很可能会因为这样做而被 Apple 审阅者拒绝。此外,此过程可能会在 iPhone 操作系统的任何小版本中完全改变,可能会导致用户崩溃,直到您发布更新来修复该行为。
不过,决定取决于您,如果您仍然想采取这些不建议的步骤,那就这样做。
You can set a different title for your MFMailComposeViewController with a single line, like so.
However, this implementation effectively relies on undocumented features of MFMailComposeViewController. You're accessing the navigationItem of a private class (_MFMailComposeRootViewController) and changing its title to something other than the mail subject. I echo Art Gillespie's sentiment in that you should not do this and are very likely to be rejected by the Apple reviewers for doing something like this. In addition, this process could change completely in any minor point release of the iPhone OS, possibly causing crashes for your users until you can release an update to fix the behavior.
The decision is up to you, though, and if you still want to take these unrecommended steps, that is how you do it.
来自 MFMailComposeViewController 类参考:
From the MFMailComposeViewController Class Reference:
消息的主题似乎提供了 iOS 8 中 MFMailComposeViewController 的标题。
It seems that the subject of the message feeds the title of MFMailComposeViewController in iOS 8.
你应该能够只获取一个视图(controller.view)并将其放置在你的控制器中......在那一刻,你没有修改任何东西,你实际上在做几乎与Apple在他们的iPad电子邮件应用程序中相同的事情撰写电子邮件...同样的事情也应该在 iPhone 上工作...
You should be able to take just a view (controller.view) and place it inside of your controller ... in that moment, you are not modifying anything and you are actually doing almost the same thing like Apple in their iPad email app when composing an email ... same thing should work on iPhone too ...
Sbrocket 的回答效果很好。这是添加标题视图(标签)的方法:
与上面的注释相同,不建议自定义
MFMailComposeViewController
...Sbrocket's answer works great. This is how to add a title view (label):
Same comments as above, it's not really recommended to customize
MFMailComposeViewController
...最有可能的是,您必须在视图层次结构中深入挖掘才能找到包含标题的原始 UINavigationBar,并在其上手动设置标题。
程序 class-dump 在这里可能会派上用场,用于确定所使用的确切类。尝试、错误和调试器很可能是您最好的选择。
Most likely, you would have to dig down in the view hierarchy to find the raw UINavigationBar that contains the title, and manually set the title on that.
The program class-dump may come in handy here for determining the exact classes used. Trial, error, and a debugger are most likely your best bet.