繁体   English   中英

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

[英]In UIImagePickerController Cancel button not showing?

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

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

我的问题:在此处输入图片说明

我的要求:在此处输入图片说明

这对我有用:

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

看起来苹果在它(iOS 10,Xcode 8)上犯了一些错误,因为无法完成更改 UIImagePickerController 的色调颜色,因为在控制器没有topItem属性或navigationController属性之前。 所以已经完成了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
        }
    }

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

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

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

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

我认为您还没有将 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;
}

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

 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 的色调颜色。

在此处输入图片说明

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

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

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

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

像魅力一样为我工作。 被这件事搞得一团糟

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

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

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

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

这对我有用:

let BarButtonItemAppearance = UIBarButtonItem.appearance()

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

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

    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)

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

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

let picker = UIImagePickerController()

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

斯威夫特 5:

  imagePicker.modalPresentationStyle = .fullScreen

试试这个代码:

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

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

picker.setNavigationBarHidden(false, animated: true)

暂无
暂无

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

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