iOS 11 上的 ImagePickerController 不显示 rightBarButton 但它可以在点击时使用

ImagePickerController on iOS 11 not showing rightBarButton But it works on tapping

当 运行 应用程序在 iOS 11.I 上使用 Xcode 9 时,我遇到了一个奇怪的问题,以便在导航栏中向右显示取消按钮以返回父项controller.It 在 iOS 10.3 之前工作正常。

我试过设置导航栏的色调 & .topItem.rightBarButtonItem。通过处理导航委托方法,当我们 select 任何相册并返回相册列表视图时,取消按钮变得可见。

这是我用来显示取消按钮的代码

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    //Add cancel button since it was not visible even applying tint color to imagepickerviewcontroller
    UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [customButton setFrame:CGRectMake(0, 0, 80, 44)];
    [customButton setTitleColor:self.tintColor forState:UIControlStateNormal];
    [customButton.titleLabel setTextAlignment:NSTextAlignmentRight];
    [customButton setTitle:NSLocalizedString(@"CF_CANCEL", @"Cancel") forState:UIControlStateNormal];
    [customButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];

    UINavigationBar *navigationBar = navigationController.navigationBar;
    navigationBar.barStyle = UIBarStyleDefault;
    navigationBar.tintColor = [UIColor redColor];
    UINavigationItem *pickerNavBarTopItem = navigationBar.topItem;

    UIBarButtonItem *cancelBarItem = [[UIBarButtonItem alloc]initWithCustomView:customButton];
    pickerNavBarTopItem.rightBarButtonItem = cancelBarItem;

}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(nonnull UIViewController *)viewController animated:(BOOL)animated {

    //Add cancel button since it was not visible even applying tint color to imagepickerviewcontroller
    UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [customButton setFrame:CGRectMake(0, 0, 80, 44)];
    [customButton setTitleColor:self.tintColor forState:UIControlStateNormal];
    [customButton.titleLabel setTextAlignment:NSTextAlignmentRight];
    [customButton setTitle:NSLocalizedString(@"CF_CANCEL", @"Cancel") forState:UIControlStateNormal];
    [customButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];

    UINavigationBar *navigationBar = navigationController.navigationBar;
    navigationBar.barStyle = UIBarStyleDefault;
    navigationBar.tintColor = [UIColor redColor];
    UINavigationItem *pickerNavBarTopItem = navigationBar.topItem;

    UIBarButtonItem *cancelBarItem = [[UIBarButtonItem alloc]initWithCustomView:customButton];
    pickerNavBarTopItem.rightBarButtonItem = cancelBarItem;
}

- (void)cancelButtonAction{

    if (self.imageCompletionBlock != nil) {
        self.imageCompletionBlock(nil, self.takePhotoOption);

    }
    [self.imagePickerController dismissViewControllerAnimated:YES completion:nil];
}

我尝试在 UIImagePickerController 扩展中覆盖 viewWillLayoutSubviews

@implementation UIImagePickerController (FFGNavbar)

    -(void)viewWillLayoutSubviews
    {
        [super viewWillLayoutSubviews];
        [self.navigationBar setTintColor:[UIColor cf_themeColor]];
        self.navigationBar.topItem.rightBarButtonItem.tintColor = [UIColor cf_themeColor];
        [self.navigationBar.topItem.rightBarButtonItem  setEnabled:true];

    }

请帮助我我还能做些什么来让它像以前一样工作。

当我点击右上角然后这个取消按钮变得可见时,我截取了屏幕截图。

感谢您提供宝贵的答案。我能够通过从 UINavigationController 子类中删除 UIBarButtonItem 外观代码并与设置自定义按钮一起使用来解决问题。