目标c - UIActivityViewController和navigationItem.leftBarButtonItem的组合

时间:2015-11-04 12:11:57

标签: ios objective-c uinavigationbar uiactivityviewcontroller

我正在使用导航栏,我正在使用代码在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个像素 - 就像这张图片一样 enter image description here

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

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

可能是什么问题?

由于

1 个答案:

答案 0 :(得分:0)

将后退按钮设置为:

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

}