0

I am facing weird issue while running app using Xcode 9 on iOS 11.I've to show cancel button in navigation bar right to move back on parent controller.It was working fine till iOS 10.3 .

I've tried with setting tint color of navigation bar & .topItem.rightBarButtonItem . With handling navigation delegate methods ,cancel button becomes visible when we select any album & comes back album list view.

here is the code, i've used for showing cancel button

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

I've tried with overriding viewWillLayoutSubviews in UIImagePickerController extension

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

    }

Please help me what else can i do to make it work like before.

I've taken screenshot when i tapped on top right then this cancel button becomes visible . enter image description here

Ellen
  • 5,180
  • 1
  • 12
  • 16
  • The `UIImagePickerController` has a cancel button, why do you have to override it? – Bannings Sep 17 '17 at 10:31
  • But it is not visible now. So after overriding atleast i can see that while moving forward/coming back. – Ellen Sep 17 '17 at 10:57
  • 1
    May be you use UIAppereance somewhere in the application – Andrew Romanov Sep 17 '17 at 10:58
  • yes Andrew ,I am using UIAppearance for Application nav bar. – Ellen Sep 17 '17 at 10:59
  • will it be causing any issue with imagePicker nav bar? – Ellen Sep 17 '17 at 11:05
  • 1
    @Ellen UIAppearance is the issue. You have two options; first one is subclass `UINaviationBar`, set the appearance only for the subclass and use it all over you app. This way system components like the `UIImagePickerController` will remain "default. Second option is to restore appearance before showing those and restore it when dismissing them. – GuillermoMP Sep 17 '17 at 18:52
  • Thanks @GuillermoMP . We were setting bar button appearance for app custom nav bar & it starts getting reflected for default nav bar & tool bar too. – Ellen Sep 18 '17 at 06:20

1 Answers1

0

Thanks for valuable answers. I was able to resolve issue by removing UIBarButtonItem appearance code from UINavigationController subclass & used with setting custom button.

Ellen
  • 5,180
  • 1
  • 12
  • 16