0

我在 iOS 11 上使用 Xcode 9 运行应用程序时遇到了奇怪的问题。我必须在导航栏中显示取消按钮才能移回父控制器。它在 iOS 10.3 之前工作正常。

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

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

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

    }

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

当我点击右上角时,我已经截取了屏幕截图,然后这个取消按钮变得可见。 在此处输入图像描述

4

1 回答 1

0

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

于 2017-09-18T06:23:06.217 回答