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 .
Thanks for valuable answers. I was able to resolve issue by removing UIBarButtonItem appearance code from UINavigationController subclass & used with setting custom button.