In UIImagePickerController Cancel button not showing?
Asked Answered
T

17

14

here i m using below code. please help me if anybody know this issue. and i tried below urls also, but its not working. please help me

iOS7 UIImagePickerController cancel button disappear UIImagePickerController inside UIPopoverController doesn't show Cancel button on iOS7

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
picker.navigationBar.tintColor = [UIColor redColor];
picker.navigationBar.barStyle = UIBarStyleBlackOpaque;
picker.navigationBar.topItem.rightBarButtonItem.tintColor = [UIColor blackColor];

My Issue: enter image description here

My Req: enter image description here

Trask answered 10/2, 2015 at 10:55 Comment(4)
Do you use appearance category for UINavigationBar in your app? Or any other?Housewifery
@Venkatesh: Please check the answer provided by users and up vote for best answer. also accepts some one's answer if your problem got solved by using the answerAmrita
Did you manage to find a solution for thisStochastic
Please refer this answer if every case doesn't work https://mcmap.net/q/812867/-in-uiimagepickercontroller-cancel-button-not-showingGithens
L
8

This is what worked for me:

    present(imagePicker, animated: true, completion: {
        imagePicker.navigationBar.topItem?.rightBarButtonItem?.tintColor = .black
    })
Labiovelar answered 15/5, 2017 at 18:58 Comment(0)
T
3

Looks like apple made some mistake with it (iOS 10, Xcode 8) because just changing tint color of UIImagePickerController could not be done, cause, before controller isn't have topItem property, or navigationController property. So have done the changes in UIImagePickerController extension. But I checked navigationController and topItem in those overrided methods: viewDidLoad, viewWillAppear, viewDidAppear. but it still was nil. So i decide to check it in viewWillLayoutSubviews, and voila! It's wasn't nil, so we can set bar tint color of exact rightBarButtomItem here!

Here is example:

    extension UIImagePickerController {
        open override func viewWillLayoutSubviews() {
            super.viewWillLayoutSubviews()
            self.navigationBar.topItem?.rightBarButtonItem?.tintColor = UIColor.black 
            self.navigationBar.topItem?.rightBarButtonItem?.isEnabled = true
        }
    }

Exampel on simulator (but it tested on )

And don't forget to call super.viewWillLayoutSubviews, it's very important ;-) EDIT: But it still has problems when return to the albums screen..

Tracitracie answered 23/2, 2017 at 11:17 Comment(1)
This helped me to get cancel button. But now in camera roll not able to find bar button items. Please guide me if u find solution means. Thank You.Pungent
G
2

If every effort doesn't work then you must have to use appearance() method. So try to write following lines in any view controller(UIImagePickerController) which you want to present. You can use appearance in any class but you may have used UIBarButtonItem.appearance() for some other purposes so find that and write that code in viewDidDisappear().

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .highlighted)
Githens answered 23/5, 2018 at 14:24 Comment(0)
Q
2

Swift 4.2 Solution, Add below code into AppDelegate's didFinishLaunchingWithOptions

UINavigationBar.appearance(whenContainedInInstancesOf: [UIImagePickerController.self]).tintColor = .black
Questa answered 8/3, 2019 at 4:24 Comment(0)
A
1

I think you have not embedded viewController to Navigation Controller

Please do that and try the code: (Objective - c)

-(void)viewDidLoad
{
    [super viewDidLoad];

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePickerController.delegate = self;
    [self presentViewController:imagePickerController animated:YES completion:nil];  
}    

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
        [picker dismissViewControllerAnimated:YES completion:^{
        }];

    UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 300)];
    imageview.image=image;
}
Amrita answered 7/4, 2015 at 9:30 Comment(1)
Embed your view controller to navigation controller from storyboardAmrita
G
1

I used the same code that you posted. Its showing the "cancel" button.

 UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:NULL];
    picker.navigationBar.tintColor = [UIColor redColor];
    picker.navigationBar.barStyle = UIBarStyleBlackOpaque;
    picker.navigationBar.topItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    [self presentViewController:picker animated:YES completion:NULL];

May be you are using any custom navigation bar and you set the property for app delegate to changing the tint color of navigation nar.

enter image description here

Girlfriend answered 18/9, 2015 at 9:39 Comment(0)
C
1

Seems like update some of item properties make the button visible, so... just turn it enabled like that:

present(imagePicker, animated: true, completion: {
    self.imagePicker.navigationBar.topItem?.rightBarButtonItem?.isEnabled = true
})
Cardiology answered 14/7, 2018 at 16:39 Comment(0)
S
1

If any of the above does not work out for you, try adding this when you define your UIImagePickerController

    imagePicker.modalPresentationStyle = .popover // Or.fullScreen (Can also check cases for iPad and iPhone as per requirement)

Worked for me like a charm. Was quite bugged up by this

Stochastic answered 5/9, 2018 at 7:23 Comment(2)
Thanks, setting modalPresentationStyle to .fullscreen worked for me.Dispersal
if #available(iOS 11.0, *) { UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentBrowserViewController.self]).tintColor = nil UINavigationBar.appearance(whenContainedInInstancesOf: [UIImagePickerController.self]).tintColor = nil } Try adding this in your app delegate. I find it really useful. You won't need the above i guess after this. let me know if it helpsStochastic
P
1

Subclass UIPickerViewController as below:

class CustomImagePicker: UIImagePickerController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        UINavigationBar.appearance().tintColor = UIColor.black
        UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.init(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)], for: .normal)
    }

    override func viewWillDisappear(_ animated: Bool) {

        UINavigationBar.appearance().tintColor = UIColor.white // your color
        UIBarButtonItem.appearance().setTitleTextAttributes(nil, for: .normal)
        super.viewWillDisappear(animated)

    }

}

And use As:

func openGallary()
    {
        picker!.sourceType = UIImagePickerController.SourceType.photoLibrary
        picker!.modalPresentationStyle = .currentContext
        self.present(picker!, animated: true, completion: nil)
    }
Phobia answered 4/3, 2020 at 11:7 Comment(0)
T
1

Swift 5:

  imagePicker.modalPresentationStyle = .fullScreen
Tourer answered 14/12, 2021 at 19:44 Comment(0)
P
0

change the UIImagePickerController navigationBar.tintColor try this code :

UINavigationBar *bar = [self.navigationController navigationBar];
    [bar setTintColor:[UIColor blueColor]];
Placet answered 10/2, 2015 at 11:0 Comment(0)
C
0
UIImagePickerController *imgPicker=[[UIImagePickerController alloc]init];    
imgPicker.delegate = self;
imgPicker.navigationBar.barStyle = UIBarStyleBlackOpaque;
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPicker.allowsEditing = NO;

if([UIImagePickerController isSourceTypeAvailable: sourceType])
{
    imgPicker.sourceType=sourceType;
    [self presentViewController:imgPicker animated:YES completion:NULL];
}
Cuccuckold answered 14/2, 2015 at 6:18 Comment(0)
B
0

This worked for me:

let BarButtonItemAppearance = UIBarButtonItem.appearance()

BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: .blue), NSFontAttributeName: UIFont.boldSystemFont(ofSize: 16.0)], for: .normal)
Booking answered 26/10, 2018 at 10:35 Comment(0)
H
0

I have face this kind of same issue and after wasting lot of time i found solution. I have customise navigation bar appearance like below code that why this issue was happened.

    UINavigationBar.appearance().backIndicatorImage = UIImage(named: "ic_left")
    let attributes = [NSAttributedString.Key.font: UIFont(name: "FuturaPT-Medium", size: 16)!]
    UINavigationBar.appearance().titleTextAttributes = attributes
    UINavigationBar.appearance().setBackgroundImage(UIImage(named: "line.png"),for: .bottom,barMetrics: .default)
    UINavigationBar.appearance().shadowImage = UIImage(named: "line.png")

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .normal)

This is just custom appearance of navigation bar we just need to change last line of code because i have set title color to clear. This is really very simple. Put this code before presenting your image picker controller.

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)

And again if you don't want navigation bar button title make text color to clear. Thanks

Haggis answered 9/4, 2019 at 4:39 Comment(0)
O
0

Please try this code for swift 3 or above

let picker = UIImagePickerController()

picker.navigationBar.topItem?.rightBarButtonItem?.tintColor = UIColor.white
Oday answered 30/4, 2019 at 5:38 Comment(0)
M
-1

Try this Code:

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];

And add delegate Method: - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
  [picker dismissViewControllerAnimated:YES completion:^{
  }];

  self.img = image;
  [self.iconBtn setBackgroundImage:image forState:UIControlStateNormal];
}
Meloniemelony answered 10/2, 2015 at 11:5 Comment(1)
me too done same process, wen u pick any image from gallery @ that time this method called didFinishPickingMediaWithInfo, but my prob is before picking the image "cancel button " not displayTrask
R
-1

i think this will help you.Just paste the code inside imagePickerControllerDidCancel then dismiss it (Swift 4).

picker.setNavigationBarHidden(false, animated: true)
Rootless answered 25/2, 2019 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.