调用 MFMessageComposeViewController UINavigationBar 高出一截

2018-05-24 17:48:18 浏览数 (2)

项目告一段落,总结一下项目中遇到的坑

使用 MFMessageComposeViewController 在程序内部发送短信,代码很简单

代码语言:javascript复制
MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
controller.recipients = @[收件人手机号码1,收件人手机号码2,收件人手机号码3];
controller.body = 短信内容

controller.messageComposeDelegate = self;

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

然后是代理回调

代码语言:javascript复制
#pragma mark ---- 发短信代理
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissViewControllerAnimated:YES completion:nil];
    switch (result) {
        case MessageComposeResultSent:
            //信息传送成功
        {
            break;
        }
        case MessageComposeResultFailed:
            //信息传送失败
            
            break;
        case MessageComposeResultCancelled:
            //信息被用户取消传送
            
            break;
        default:
            break;
    }
}

但是当我全局设置 NavigationBar 的时候会影响 MFMessageComposeViewController 的 NavigationBar 全局设置 NavigationBar 错误代码如下:

代码语言:javascript复制
//NavigationBar 背景颜色
[[UINavigationBar appearance]setBarTintColor:NaviBarTintColor];


[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:NaviBarTintColor] forBarMetrics:UIBarMetricsDefault];

[[UINavigationBar appearance] setShadowImage:[UIImage new]];

//NavigationBar 文字属性
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:WhiteColor}];

修改方法给NavigationViewController 抽出一个基类 在 viewDidLoad 全局修改NavigationBar 如下

代码语言:javascript复制
[self.navigationBar setTintColor:WhiteColor];

[self.navigationBar setBackgroundImage:[UIImage imageWithColor:NaviBarTintColor] forBarMetrics:UIBarMetricsDefault];

[self.navigationBar setShadowImage:[UIImage new]];

[self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:WhiteColor}];

之后会发现 MFMessageComposeViewController 的NavigationBar 是系统主题的,可以修改为自己的主题,在创建的时候加入

代码语言:javascript复制
MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
controller.recipients = @[telphone];
controller.body = self.friendProfitModel.inviteBuyDebtSmsContent;
controller.navigationBar.tintColor = WHColorBlack;

controller.messageComposeDelegate = self;

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

UIBarButtonItem *bar=    [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction)];


[[[[controller viewControllers] lastObject] navigationItem] setRightBarButtonItem:bar];

[[[[controller viewControllers] lastObject] navigationItem] setTitle:@"邀请好友"];

代理回调不变

好本坑完美解决

0 人点赞