简体   繁体   中英

In UIImagePickerController Cancel button not showing?

here im 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:在此处输入图片说明

My Req:在此处输入图片说明

This is what worked for me:

    present(imagePicker, animated: true, completion: {
        imagePicker.navigationBar.topItem?.rightBarButtonItem?.tintColor = .black
    })

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
        }
    }

模拟器上的示例(但它在 上进行了测试)

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

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)

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

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.

在此处输入图片说明

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
})

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

Swift 4.2 解决方案,将以下代码添加到 AppDelegate 的 didFinishLaunchingWithOptions 中

UINavigationBar.appearance(whenContainedInInstancesOf: [UIImagePickerController.self]).tintColor = .black

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)
    }

change the UIImagePickerController navigationBar.tintColor try this code :

UINavigationBar *bar = [self.navigationController navigationBar];
    [bar setTintColor:[UIColor blueColor]];
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];
}

This worked for me:

let BarButtonItemAppearance = UIBarButtonItem.appearance()

BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: .blue), NSFontAttributeName: UIFont.boldSystemFont(ofSize: 16.0)], for: .normal)

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

Please try this code for swift 3 or above

let picker = UIImagePickerController()

picker.navigationBar.topItem?.rightBarButtonItem?.tintColor = UIColor.white

斯威夫特 5:

  imagePicker.modalPresentationStyle = .fullScreen

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

我认为这会对您有所帮助。只需将代码粘贴到 imagePickerControllerDidCancel 中,然后将其关闭(Swift 4)。

picker.setNavigationBarHidden(false, animated: true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM