31

I am new to iOS development. I am trying to hide status bar in UIImagePickerController. Whenever I click on "Take photo", status bar appears. It doesn't hide. I want status bar to be hidden only in UIImagePickerController.

Here is my code,

- (IBAction)takePhoto:(UIButton *)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:picker animated:YES completion:NULL];
}


- (void)imagePickerController:(UIImagePickerController *)picker     didFinishPickingMediaWithInfo:(NSDictionary *)info
{   
    [self statusBar:YES];
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;
    [picker dismissViewControllerAnimated:YES completion:NULL];

}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}


-(void)statusBar:(BOOL)status
{
    [[UIApplication sharedApplication] setStatusBarHidden:status];
}

How to hide the status bar on UIImagePickerController?

4

13 回答 13

47

这对我来说很好:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

编辑:截至今天,我刚刚发现在您的 info.plist 中,如果您只是复制粘贴基于视图控制器的状态栏外观,它将无法正常工作......您必须在属性上按回车键,然后滚动到最后一个,这样你就可以自动完成:查看基于控制器的状态栏外观和一个布尔值,没有。我尝试了多次,但它不能仅仅复制。祝你今天过得愉快。

于 2013-09-20T08:47:12.027 回答
33

我为应用程序构建的解决方案:“查看基于控制器的状态栏外观”设置为 YES

我确实添加了类别:

//UIImagePickerController+StatusBarHidden.h
#import <UIKit/UIKit.h>

@interface UIImagePickerController (StatusBarHidden)
@end

//UIImagePickerController+StatusBarHidden.h
#import "UIImagePickerController+StatusBarHidden.h"

@implementation UIImagePickerController (StatusBarHidden)

-(BOOL) prefersStatusBarHidden {
    return YES;
}

-(UIViewController *) childViewControllerForStatusBarHidden {
    return nil;
}

@end

childViewControllerForStatusBarHidden方法很少使用,但图像选择器确实使用它,这可能会导致一些麻烦

您还可以根据其属性使用返回 YES 或 NO 的方法来实现 UIViewController 单例。然后您的视图控制器实现childViewControllerForStatusBarHidden返回上述单例。更改单例属性会自动更改应用程序中的状态栏。还有双方法 childViewControllerForStatusBarStyle


但是对于 2014 年的 iOS8,请参阅此https://stackoverflow.com/a/18960308/294884

于 2013-11-14T12:42:41.777 回答
19

我遇到了一个问题,在 iOS7 中我的状态栏没有被隐藏。我以编程方式隐藏它,它仍然显示在 iOS7 中,但在 iOS6 中运行时,状态栏会适当隐藏。您必须转到 plist 并添加以下内容:

'查看基于控制器的状态栏外观'并设置为 NO。

如果您希望状态栏重新出现在其他视图控制器中并且仅在特定 VC 上隐藏,则在 VC 加载时将状态栏设置为隐藏 YES。当 VC 将消失时,您将状态栏隐藏设置回 NO。

- (void)viewDidLoad
{

    [super viewDidLoad];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];

}

当控制器消失时,您添加以下内容以设置状态栏,使其不再隐藏并显示在下一个视图中:

-(void)viewWillDisappear:(BOOL)animated{

     [[UIApplication sharedApplication] setStatusBarHidden:NO];

}

setStatusBarHidden:withAnimation:如果你想要一些流畅的动画

于 2013-09-12T19:50:22.647 回答
18

子类 UIImagePickerController ...我的是 V1ImagePickerController ...

.m 文件如下所示:

#import "V1ImagePickerController.h"

@interface V1ImagePickerController ()

@end

@implementation V1ImagePickerController

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)   // iOS7+ only
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;

        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (UIViewController *)childViewControllerForStatusBarHidden
{
    return nil;
}

@end

childViewControllerForStatusBarHidden 是关键!

于 2013-09-19T20:32:44.967 回答
7

如果你想从 plist 中禁用状态栏,试试这个:

  1. 状态栏最初是隐藏的:是
  2. 查看基于控制器的状态栏外观:否

这对于 iOS 7 是必需的,对我有用。我不知道在 iOS7 中是否还有其他一些技术可以做到这一点。在 info.plist 中设置这两个标签。

每次您的视图控制器出现时,在 viewDidLoad 或图像选择器控制器完成时,请使用以下命令:

 - (void) imagePickerController:(UIImagePickerController *)picker1 didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
   [[UIApplication sharedApplication] setStatusBarHidden:YES];
 .
 .
 .
 .
 }
于 2013-09-12T10:55:20.723 回答
6

prefersStatusBarHidden我在上面使用了 Silvertaurus 的答案,但对我认为非常有帮助的方法进行了一些修改:

//UIImagePickerController+StatusBarHidden.h
#import <UIKit/UIKit.h>

@interface UIImagePickerController (StatusBarHidden)
@end

//UIImagePickerController+StatusBarHidden.h
#import "UIImagePickerController+StatusBarHidden.h"

@implementation UIImagePickerController (StatusBarHidden)

-(BOOL) prefersStatusBarHidden {
    if (self.sourceType==UIImagePickerControllerSourceTypeCamera) {
        return YES;
    } else {
        return NO;
    }
}

-(UIViewController *) childViewControllerForStatusBarHidden {
    return nil;
}

@end

当不显示相机时,这会使图像选择器的状态栏保持打开状态。

于 2014-03-01T19:41:51.457 回答
1

请试试这个。

• 为UIImagePickerController

• 在代理的navigationController:didShowViewController:animated: 函数中隐藏状态栏。

例如:

-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
于 2013-09-12T11:56:53.050 回答
1

尝试这个

[[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];

还要检查这个讨论

于 2013-09-12T09:45:11.987 回答
1

你可以用一个类别来做:

@interface UIImagePickerController (HideStatusBar)

@end


@implementation UIImagePickerController (HideStatusBar)

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (UIViewController *)childViewControllerForStatusBarHidden
{
    return nil;
}

@end

来源:https ://gist.github.com/psobko/9493473

于 2016-04-14T15:44:48.460 回答
0

我想我以一种非常简单的方式解决了这个问题,没有子类化和使用 plist。它只对 UIImagePickerController 隐藏。

此示例仅用于显示照片库,但我想您可以使用 uiimagepickercontroller 以相同的方式将其应用到任何地方

- (void)showGallary
{
  [CATransaction begin];
  [CATransaction setCompletionBlock:^{
      [[UIApplication sharedApplication] setStatusBarHidden:YES];
      [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  }];

  imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

  [CATransaction commit];
}
于 2014-09-11T06:39:02.257 回答
0

如果您想继续使用,请尝试我在此处发布的答案ViewController-Based Status Bar Appearance

于 2013-10-05T16:34:35.567 回答
0
-(IBAction)takePhoto:(UIButton *)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self statusBar:TRUE];
    [self presentViewController:picker animated:YES completion:NULL];
}


-(void)imagePickerController:(UIImagePickerController *)picker      didFinishPickingMediaWithInfo:(NSDictionary *)info
{        
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;
    [self statusBar:FALSE];
    [picker dismissViewControllerAnimated:YES completion:NULL];
}


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self statusBar:FALSE];
    [picker dismissViewControllerAnimated:YES completion:NULL];
}


-(void)statusBar:(BOOL)status
{
    [[UIApplication sharedApplication] setStatusBarHidden:status];
}

这可能会帮助你实现你想要的。

于 2013-09-12T09:48:48.933 回答
0

就我而言,我必须使用 presentViewController 来显示 UIImagePickerViewController (iOS7)。

1- 在 .plist 中将基于视图控制器的状态栏外观设置为 NO 2- 为 UIImagePickerController 和 viewDidLayoutSubviews 创建一个类别:

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

3- Added the two following methods to the category:

- (BOOL)prefersStatusBarHidden{
    return YES;
}

- (UIViewController *)childViewControllerForStatusBarHidden{
    return nil;
}

I hope this will help.

于 2014-05-19T08:19:43.823 回答