Reputation: 35793
I have an App where it displays a UIImagePickerController on launch. The controller displays the cancel button, but there is nothing to cancel to. Is there any way to remove the button?
NOTE: This is not a duplicate of Can the Cancel button be removed from a UIImagePickerController in OS 3.2?. The answer there for iOS 3.2 does not work for me in iOS 5.
Upvotes: 0
Views: 2388
Reputation: 71
For iOS 7 and 8 the above solution will not work,
There is small change that should be done because we cannot use navigation item from navigation bar. The following will work like a charm
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
viewController.navigationItem.rightBarButtonItems = nil;
}
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
viewController.navigationItem.rightBarButtonItems = nil;
}
Upvotes: 1
Reputation: 36
I try to put UIImagePickerController not modally, but in original way (as we do it with ordinary custom UIViewController). So, I need no the cancel button in the UINavigationBar.
There are no documented ways to remove the cancel button. But here I found some idea. I just added this code in my AppDelegate.m file:
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
for (UINavigationItem *item in navigationController.navigationBar.subviews) {
if ([[item title] compare:@"Cancel"] == 0) {
UIButton *button = (UIButton *)item;
[button setHidden:YES];
}
}
}
So the cancel button is hidden now. But if you pick some folder in you photo albums the next view will be with that button (and it is not hidden). Then I tried to add code like this:
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
for (UINavigationItem *item in navigationController.navigationBar.subviews) {
if ([[item title] compare:@"Cancel"] == 0) {
UIButton *button = (UIButton *)item;
[button setHidden:YES];
}
}
}
I get the cancel button is hidden in all navigation stack inside UIImagePickerController's navigation.
But while the new view is appearing (animation transition) the cancel button is appearing too.
I don't know how to fix this problem.
However I think that it is a tricky and inefficient approach. Because it can break your application in the future iOS updates. So you can use the answer to that question. But it is different approach at all.
P.S. Sorry for my language.
Upvotes: 2