如何在UIImagepickercontroller中隐藏状态栏?

时间:2013-09-12 09:40:45

标签: ios objective-c iphone ios7 statusbar

我是iOS开发的新手。我正在尝试隐藏UIImagePickerController中的状态栏。每当我点击“拍照”时,都会出现状态栏。它没有隐藏。我希望状态栏仅隐藏在UIImagePickerController中。

这是我的代码,

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

如何隐藏UIImagePickerController上的状态栏?

14 个答案:

答案 0 :(得分:47)

这对我来说很好:

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

编辑:截至今天,我刚刚在你的info.plist中发现,如果你只是复制粘贴基于视图控制器的状态栏外观那里它将无法工作......你必须在属性上点击输入,并滚动到它们中的最后一个,这样你就可以自动完成:查看基于控制器的状态栏外观和一个布尔值,没有。我尝试了多次,但它只是复制不起作用。祝你有愉快的一天。

答案 1 :(得分:32)

我找到的应用程序解决方案围绕: “查看基于控制器的状态栏外观”设置为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单例。 然后你的View controleller实现 childViewControllerForStatusBarHidden 返回上面的单例。 更改单身人士属性会自动更改应用中的状态栏。还有双方法 childViewControllerForStatusBarStyle


然而,对于2014年,iOS8,请参阅此https://stackoverflow.com/a/18960308/294884

答案 2 :(得分: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:如果你想要一些流畅的动画

答案 3 :(得分:17)

子类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是关键!

答案 4 :(得分:7)

如果要从plist中禁用状态栏,请尝试以下操作:

  1. 状态栏最初隐藏:是
  2. 查看基于控制器的状态栏外观:否
  3. 这对于iOS 7来说是必要的,对我有用。我不知道在iOS7中是否还有其他一些技术可以做到这一点。在info.plist中设置这两个标签。

    每次出现viewcontroller时,在viewDidLoad中或图像选择器控制器完成时,请使用:

     - (void) imagePickerController:(UIImagePickerController *)picker1 didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
    {
       [[UIApplication sharedApplication] setStatusBarHidden:YES];
     .
     .
     .
     .
     }
    

答案 5 :(得分:6)

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

//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

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

答案 6 :(得分:1)

试试这个

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

同时检查此discussion

答案 7 :(得分:1)

请试一试。

•设置UIImagePickerController

的代理人

•隐藏代理人的状态栏 navigationController:didShowViewController:animated:function。

E.G:

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

答案 8 :(得分:1)

您可以使用以下类别执行此操作:

@interface UIImagePickerController (HideStatusBar)

@end


@implementation UIImagePickerController (HideStatusBar)

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (UIViewController *)childViewControllerForStatusBarHidden
{
    return nil;
}

@end

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

答案 9 :(得分: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];
}

可能会帮助您实现您想要的目标。

答案 10 :(得分:0)

如果您想继续使用ViewController-Based Status Bar Appearance,请尝试my answer posted here

答案 11 :(得分:0)

在我的情况下,我必须使用presentViewController来显示UIImagePickerViewController(iOS7)。

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

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

3-在该类别中添加了以下两种方法:

- (BOOL)prefersStatusBarHidden{
    return YES;
}

- (UIViewController *)childViewControllerForStatusBarHidden{
    return nil;
}

我希望这会有所帮助。

答案 12 :(得分:0)

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

这个例子仅用于调出照片,但我想你可以用同样的方式将它应用到uiimagepickercontroller的任何地方

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

  imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

  [CATransaction commit];
}

答案 13 :(得分:0)

我想让状态栏一直隐藏,打开图像选择器显示它。为了再次隐藏它,以下版本适用于iOS 8:

-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
    }];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
    }];

    // do stuff with photo
}

我将View controller-based status bar appearance设置为NO