iPhone - 以编程方式删除状态栏

时间:2012-03-20 21:08:16

标签: iphone ios uiimagepickercontroller statusbar

我制作了一个实现iPhone相机的应用程序。 当用户完成选择图像时,状态栏会重新出现! 我如何确保状态栏保持隐藏状态?

这是我的代码:

-(IBAction)pickImage:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;

picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
background.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

}

如果我做错了什么,请指出来! 谢谢, Rafee

6 个答案:

答案 0 :(得分:29)

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];

如果有的话,您可以选择其他动画风格。

答案 1 :(得分:20)

在iOS 7中,UIViewController上有一个方法,“prefersStatusBarHidden”。要隐藏状态栏,请将此方法添加到视图控制器并返回YES:

- (BOOL) prefersStatusBarHidden
{
    return YES;
}

答案 2 :(得分:4)

在这种情况下,我们正在使用两个步骤

第一步:                 添加info.plist:“查看基于控制器的状态栏外观”,值为“NO”

第二步:使用/使用UIImagePickerController委托

调用此代码
 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
     if([navigationController isKindOfClass:[UIImagePickerController class]])
         [[UIApplication sharedApplication] setStatusBarHidden:YES]; 
 }

答案 3 :(得分:1)

使用iOS 7及更高版本,您可以使用以下代码隐藏和取消隐藏状态栏

@interface ViewController()

@property (nonatomic, getter=isStatusBarHidden) BOOL statusBarHidden;

@end

@implementation ViewController


  ... other codes

- (BOOL)prefersStatusBarHidden {
    return self.isStatusBarHidden;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationFade;
}

- (void)hideStatusBar {
    self.statusBarHidden = YES;
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)showStatusBar {
    self.statusBarHidden = NO;
    [self setNeedsStatusBarAppearanceUpdate];
}

@end

答案 4 :(得分:0)

与UIImagePicker相关联的UIViewController的解散机制似乎存在一个错误,其中sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum。

调用dismissModalViewController的时刻(加上完成的方法:) UIApplication的状态栏隐藏属性立即从YES更改为NO,并且在踩到解雇的时刻绘制...

这对于使用全屏视图的应用来说非常明显。我当前的应用程序项目,并且我在呈现之前控制视图控制器视图的框架,因此UIImagePicker不是全屏。这使得这个bug非常明显。我花了4-5个小时来确定原因,这是最终的确定结论,并且不会发生对于sourceType Camera或PhotoLibrary的错误。

因此,如果您想要一个完美的全屏应用并希望呈现无错误的UIImagePicker,请避免使用UIImagePickerControllerSourceTypeSavedPhotosAlbum

答案 5 :(得分:0)

Grand Central dispatch是您的朋友,使用此方法,当显示选择器时或之后,您将看不到状态栏

- (void)hideStatusBar
{
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
    {
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self hideStatusBar];
    double delayInSeconds = 0.2;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self hideStatusBar];
    });
}