Objective c - UIActivityViewController 和 navigationItem.leftBarButtonItem 的组合

Objective c - Combination of UIActivityViewController and navigationItem.leftBarButtonItem

我正在使用导航栏,我正在使用代码将 viewDidLoad 中的默认后退按钮更改为我的按钮:

_backButton =[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed)];
[_backButton setTintColor:[UIColor whiteColor]];
[_backButton setImage:[UIImage imageNamed:@"Back-icon.png"]];
self.navigationItem.leftBarButtonItem = _backButton;

我有一个 UIButton 用于分享,当我点击它时,代码是 运行:

    UIActivityViewController *activityViewController =
    [[UIActivityViewController alloc] initWithActivityItems:@[@"test"]
                                      applicationActivities:nil];
    [self presentViewController:activityViewController
                       animated:YES
                     completion:^{ 
}];

现在,当我进入页面时,它看起来还不错,但是当我点击分享按钮时,UIActivityViewController 出现了,导航中的后退按钮跳到底部 50 像素 - 就像这张图片

当我使用默认后退按钮时,它不是附加的。

我尝试将 UIActivityViewController 打开的动画更改为 NO,但没有帮助。

可能是什么问题?

谢谢

将您的后退按钮设置为:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_arrow"] style:UIBarButtonItemStylePlain target:self.navigationController action:@selector(popViewControllerAnimated:)];

无需创建后退按钮方法,如果您在转到上一个控制器时不更新任何对象值或任何 UI。 如果您为选择器设置任何操作,则使用相应的目标。

当你呈现 ActivityController 时:

- (IBAction)commonSharing:(id)sender
{
    NSString *strTextToShare = @"Hi... Friends";
    UIImage *imgToShare = [UIImage imageNamed:@"wall1.png"];
    NSURL *urlToShare = [NSURL URLWithString:@"https://www.apple.com"];

    UIActivityViewController *shareController = [[UIActivityViewController alloc] initWithActivityItems:@[strTextToShare, imgToShare, urlToShare] applicationActivities :nil];
    //shareController.excludedActivityTypes = @[UIActivityTypeMessage, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll]; // If you want to restrict some of apps to sharing
    [self presentViewController:shareController animated: YES completion: nil];

}