在UIImagePickerController取消按钮不显示?

问题描述 投票:10回答:15

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

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

我的问题:

我的需求:

ios objective-c ios7 uiimagepickercontroller
15个回答
7
投票

这对我有用:

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

0
投票

这对我有用:

let BarButtonItemAppearance = UIBarButtonItem.appearance()

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

0
投票

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

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

0
投票

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

    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.black],for:。normal)

如果您不想要导航栏按钮标题,请将文本颜色清除。谢谢


0
投票

请尝试使用swift 3或更高版本的代码

let picker = UIImagePickerController()

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

-1
投票

试试这个代码:

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

-1
投票

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

picker.setNavigationBarHidden(false, animated: true)

2
投票

看起来苹果犯了一些错误(iOS 10,Xcode 8),因为在控制器没有topItem属性或navigationController属性之前,只能改变UIImagePickerController的色调颜色,导致。所以在UIImagePickerController extension做了变化。但我用那些压倒一切的方法检查了navigationControllertopItemviewDidLoadviewWillAppearviewDidAppear。但它仍然是nil。所以我决定在viewWillLayoutSubviews检查它,瞧!它不是零,所以我们可以在这里设置精确rightBarButtomItem的条纹色调!

这是一个例子:

    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 )

并且不要忘记打电话给super.viewWillLayoutSubviews,这非常重要;-)编辑:但是当它返回专辑屏幕时仍然有问题..


2
投票

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

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

1
投票

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

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

可能是您正在使用任何自定义导航栏,并且您将app delegate的属性设置为更改导航的色调颜色。

enter image description here


1
投票

我认为你还没有将viewController嵌入到Navigation Controller中

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

1
投票

似乎更新一些项属性使按钮可见,所以...只需将其启用如下:

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

1
投票

如果以上任何方法无效,请在定义UIImagePickerController时尝试添加此项

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

为我工作就像一个魅力。被这件事搞砸了


0
投票

更改UIImagePickerController navigationBar.tintColor尝试此代码:

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

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];
}
© www.soinside.com 2019 - 2024. All rights reserved.