【问题标题】:In UIImagePickerController Cancel button not showing?在 UIImagePickerController 取消按钮不显示?
【发布时间】:2015-04-10 09:07:57
【问题描述】:

我在这里使用下面的代码。如果有人知道这个问题,请帮助我。 我也尝试了以下网址,但它不起作用。请帮帮我

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

我的问题:

我的要求:

【问题讨论】:

  • 您在您的应用程序中使用appearance 类别作为UINavigationBar 吗?还是其他?
  • @Venkatesh:请检查用户提供的答案并投票选出最佳答案。如果您的问题通过使用答案得到解决,也接受某人的答案
  • 你找到解决方案了吗
  • 如果每个案例都不起作用,请参考这个答案stackoverflow.com/a/50490924/1201274

标签: ios objective-c ios7 uiimagepickercontroller


【解决方案1】:

这对我有用:

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

【讨论】:

    【解决方案2】:

    看起来苹果犯了一些错误(iOS 10,Xcode 8),因为无法仅更改 UIImagePickerController 的色调颜色,因为在控制器没有topItem 属性或navigationController 属性之前。在UIImagePickerController extension 中进行了更改。但我在那些被覆盖的方法中检查了navigationControllertopItemviewDidLoadviewWillAppearviewDidAppear。但它仍然是nil。所以我决定在viewWillLayoutSubviews 中查看它,瞧!它不是 nil,所以我们可以在这里设置确切 rightBarButtomItem 的条形颜色!

    示例如下:

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

    别忘了打电话给super.viewWillLayoutSubviews,这很重要;-) 编辑:但是返回相册屏幕时仍然有问题..

    【讨论】:

    • 这帮助我获得了取消按钮。但现在在相机胶卷中找不到条形按钮项目。如果您找到解决方案,请指导我。谢谢。
    【解决方案3】:

    如果所有努力都不起作用,那么您必须使用外观()方法。因此,请尝试在您要呈现的任何视图控制器(UIImagePickerController)中编写以下行。您可以在任何类中使用外观,但您可能已将 UIBarButtonItem.appearance() 用于其他目的,因此请在 viewDidDisappear() 中找到并编写该代码。

    UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .normal)
    UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .highlighted)
    

    【讨论】:

      【解决方案4】:

      我使用了您发布的相同代码。它显示“取消”按钮。

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

      可能您正在使用任何自定义导航栏,并且您将应用代理的属性设置为更改导航 nar 的色调。

      【讨论】:

        【解决方案5】:

        我认为您还没有将 viewController 嵌入到导航控制器中

        请这样做并尝试代码:(目标 - 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;
        }
        

        【讨论】:

        • 将您的视图控制器嵌入到故事板中的导航控制器
        【解决方案6】:

        似乎更新一些项目属性使按钮可见,所以...只需像这样启用它:

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

        【讨论】:

          【解决方案7】:

          如果以上任何一项都不适合您, 定义UIImagePickerController时尝试添加它

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

          像魅力一样为我工作。被这玩意儿搞砸了

          【讨论】:

          • 谢谢,将 modalPresentationStyle 设置为 .fullscreen 对我有用。
          • if #available(iOS 11.0, *) { UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentBrowserViewController.self]).tintColor = nil UINavigationBar.appearance(whenContainedInInstancesOf: [UIImagePickerController.self]).tintColor = nil尝试在您的应用委托中添加它。我觉得它真的很有用。我猜在此之后您将不需要上述内容。让我知道它是否有帮助
          【解决方案8】:

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

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

          【讨论】:

            【解决方案9】:

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

            并用作:

            func openGallary()
                {
                    picker!.sourceType = UIImagePickerController.SourceType.photoLibrary
                    picker!.modalPresentationStyle = .currentContext
                    self.present(picker!, animated: true, completion: nil)
                }
            

            【讨论】:

              【解决方案10】:

              更改 UIImagePickerController navigationBar.tintColor 试试这个代码:

              UINavigationBar *bar = [self.navigationController navigationBar];
                  [bar setTintColor:[UIColor blueColor]];
              

              【讨论】:

                【解决方案11】:
                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];
                }
                

                【讨论】:

                  【解决方案12】:

                  这对我有用:

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

                  【讨论】:

                    【解决方案13】:

                    我也遇到过同样的问题,在浪费了很多时间之后,我找到了解决方案。我已经自定义了导航栏的外观,如下面的代码,说明了为什么会发生这个问题。

                        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],用于:.normal)

                    这只是导航栏的自定义外观,我们只需要更改最后一行代码,因为我已将标题颜色设置为清除。这真的很简单。在展示您的图像选择器控制器之前放置此代码。

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

                    如果您不希望导航栏按钮标题再次清除文本颜色。 谢谢

                    【讨论】:

                      【解决方案14】:

                      请在 swift 3 或更高版本上尝试此代码

                      let picker = UIImagePickerController()
                      
                      picker.navigationBar.topItem?.rightBarButtonItem?.tintColor = UIColor.white
                      

                      【讨论】:

                        【解决方案15】:

                        斯威夫特 5:

                          imagePicker.modalPresentationStyle = .fullScreen
                        

                        【讨论】:

                          【解决方案16】:

                          试试这个代码:

                          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

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

                          【讨论】:

                          • 我也做了同样的过程,你从画廊@选择任何图像当时这个方法称为 didFinishPickingMediaWithInfo,但我的问题是在选择图像之前“取消按钮”不显示
                          【解决方案17】:

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

                          picker.setNavigationBarHidden(false, animated: true)
                          

                          【讨论】:

                            猜你喜欢
                            • 1970-01-01
                            • 1970-01-01
                            • 2018-06-25
                            • 2019-06-23
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 2012-02-01
                            相关资源
                            最近更新 更多