简体   繁体   English

在 UIImagePickerController 中,取消按钮未显示?

[英]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 iOS7 UIImagePickerController 取消按钮消失UIPopoverController 内的 UIImagePickerController 在 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.看起来苹果在它(iOS 10,Xcode 8)上犯了一些错误,因为无法完成更改 UIImagePickerController 的色调颜色,因为在控制器没有topItem属性或navigationController属性之前。 So have done the changes in UIImagePickerController extension .所以已经完成了UIImagePickerController extension的更改。 But I checked navigationController and topItem in those overrided methods: viewDidLoad , viewWillAppear , viewDidAppear .但是我在这些重写的方法中检查了navigationControllertopItemviewDidLoadviewWillAppearviewDidAppear but it still was nil .但它仍然nil So i decide to check it in viewWillLayoutSubviews , and voila!所以我决定在viewWillLayoutSubviews检查它,瞧! It's wasn't nil, so we can set bar tint color of exact rightBarButtomItem here!它不是零,所以我们可以在这里设置精确 rightBarButtomItem 的条形着色颜色!

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..并且不要忘记调用super.viewWillLayoutSubviews ,这非常重要;-) 编辑:但是返回相册屏幕时仍然有问题..

If every effort doesn't work then you must have to use appearance() method.如果一切努力都不起作用,那么您必须使用 appearance() 方法。 So try to write following lines in any view controller(UIImagePickerController) which you want to present.因此,尝试在您想要呈现的任何视图控制器(UIImagePickerController)中编写以下行。 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() 用于其他目的,因此请找到并在 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我认为您还没有将 viewController 嵌入到导航控制器中

Please do that and try the code: (Objective - c)请这样做并尝试代码:(目标 - 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.可能是您正在使用任何自定义导航栏,并且您将应用程序委托的属性设置为更改导航 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如果以上任何一项都不适合您,请尝试在定义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:子类 UIPickerViewController 如下:

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 :更改 UIImagePickerController navigationBar.tintColor 试试这个代码:

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) 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) 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请在 swift 3 或更高版本中尝试此代码

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UIImagePickercontroller取消按钮在iPad中不显示 - UIImagePickercontroller cancel button not display in iPad 更改UIImagePickerController中的取消按钮文本 - Change cancel button text in UIImagePickerController UIImagePickerController取消按钮无法正常工作的问题 - Problems with UIImagePickerController cancel button not working UIImagePickerController后退按钮/取消按钮不可见 - UIImagePickerController back button/cancel button is not visible 压缩视频时UIImagePickerController取消按钮不起作用 - UIImagePickerController cancel button not working when compressing video iOS7中带有取消按钮的弹出式窗口中的UIImagePickerController - UIImagePickerController within a popover with cancel button in iOS7 如何在UIImagePickerController中更改取消按钮标题? - How to change cancel button title in UIImagePickerController? 允许使用UIImagePickerController捕获照片,直到单击“取消”按钮为止 - Allow capturing photos using UIImagePickerController until cancel button clicked 使用UIImagePickerController自定义“使用”,“取消”,“重拍”和“倒车”按钮事件 - Custom “Use”, “Cancel”,“Retake” and “Reverse Camera” button event using UIImagePickerController 选择取消按钮错误后的iOS 11 UIImagePickerController - iOS 11 UIImagePickerController after selection cancel button bug
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM