iOS 7 StatusBar问题,当我使用UIImagePickerController时允许编辑

时间:2014-01-11 06:08:08

标签: ios ios7 uiimagepickercontroller uistatusbar

我研究相关的iOS 7状态 - Bar Maintain,但我在使用UIImagePickerController时遇到了问题。允许编辑“编辑窗口”在顶部栏显示20像素空间。

我使用了代码,首先我在UIViewControllerBasedStatusBarAppearance中将NO设置为info.plist并设置了委托方法:

Appdelegate.h

@property (retain, nonatomic) UIWindow *background;

Appdelegate.m

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {

    [application setStatusBarStyle:UIStatusBarStyleLightContent];
    self.window.clipsToBounds =YES;
    self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);

    self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);

    background = [[UIWindow alloc] initWithFrame: CGRectMake(0, 0, self.window.frame.size.width, 20)];
    background.backgroundColor =[UIColor redColor];
    [background setHidden:NO];
}

在我的家庭视图控制器中没有任何效果,所以我放入了我的home viewController,一种用于更改状态栏背景颜色的方法:

-(void)viewWillLayoutSubviews{

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    self.view.clipsToBounds = YES;
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenHeight = screenRect.size.height;
    self.view.frame = CGRectMake(0, 20, self.view.frame.size.width,screenHeight-20);
    self.view.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"userIsMale"]) {
        [tempWindow setBackgroundColor:[UIColor colorWithRed:(46.0/255.0) green:(134.0/255.0) blue:(255.0/255.0) alpha:1]];
    }
    else {
        [tempWindow setBackgroundColor:[UIColor colorWithRed:(246.0/255.0) green:(26.0/255.0) blue:(113.0/255.0) alpha:1]];
    }

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

现在,当我出现UIImagePickerController时,允许编辑窗口显示如下:

Enter image description here

我在使用此解决方案时尝试了隐藏状态栏UIImagePickerController

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

并在ViewWillApear显示状态条形码。

我得到了这种结果:

Enter image description here

我在哪里做错了,我该如何解决?

5 个答案:

答案 0 :(得分:3)

尝试使用:

-(BOOL)prefersStatusBarHidden { return YES; }

它会隐藏状态栏。

在iOS 7上,如果您想使用setStatusBarHidden:,则需要在info.plist中将View controller-based status bar appearance设置为NO。

我希望它会给你一些提示。


编辑:

我发现了问题。

这是第一次在viewWillAppear上显示homeViewController时的日志:

(lldb) po [[UIApplication sharedApplication] windows]
<__NSArrayM 0x8a74560>(
<UIWindow: 0x8e33360; frame = (0 20; 320 548); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x8e348b0>; layer = <UIWindowLayer: 0x8e3c6e0>>,
<UIWindow: 0x8c1db40; frame = (0 0; 320 20); gestureRecognizers = <NSArray: 0x8c1e170>; layer = <UIWindowLayer: 0x8c1da80>>
)

这是解雇imagePicker并调用viewWillAppear后的日志:

(lldb) po [[UIApplication sharedApplication] windows]
<__NSArrayM 0x8c1a700>(
<UIWindow: 0x8e33360; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x8e348b0>; layer = <UIWindowLayer: 0x8e3c6e0>>,
<UIWindow: 0x8c1db40; frame = (0 0; 320 20); gestureRecognizers = <NSArray: 0x8c1e170>; layer = <UIWindowLayer: 0x8c1da80>>,
<UITextEffectsWindow: 0x8c53380; frame = (0 0; 320 568); hidden = YES; opaque = NO; gestureRecognizers = <NSArray: 0x8c538a0>; layer = <UIWindowLayer: 0x8c53520>>
)

默认窗口大小已更改。这就是状态栏无法显示的原因。

我编辑了这样的代码,它对我有用:

 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    if(OVER_IOS7){
        [self.navigationController.navigationBar setTranslucent:NO];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}

-(void)viewWillLayoutSubviews{

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        self.view.clipsToBounds = YES;
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        CGFloat screenHeight = screenRect.size.height;
        self.view.frame = CGRectMake(0, 20, self.view.frame.size.width,screenHeight-20);
        self.view.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

        UIWindow *defaultWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:0];

        defaultWindow.frame =  CGRectMake(0,20,320,548);
        defaultWindow.bounds = CGRectMake(0,20,320,548);

        UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];

        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"userIsMale"]) {
            [tempWindow setBackgroundColor:[UIColor colorWithRed:(46.0/255.0) green:(134.0/255.0) blue:(255.0/255.0) alpha:1]];
        }
        else {
            [tempWindow setBackgroundColor:[UIColor colorWithRed:(246.0/255.0) green:(26.0/255.0) blue:(113.0/255.0) alpha:1]];
        }

        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }
}

- (IBAction)showImagePicker:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];

    [picker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    [self presentViewController:picker animated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    }];
}

答案 1 :(得分:0)

尝试通过以下方式展示UIImagePickerController

UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
[tempWindow.rootViewController presentViewController:myImgPickerController animated:YES completion:nil];

我不知道它是否会对你有所帮助,但你可以尝试一下......:)

另一种选择是当你出现UIImagePickerController时。那时设置你的视图框架就像它最初一样,并隐藏状态栏......

答案 2 :(得分:0)

我也面临同样的问题,但只有一种方法可以克服它。

转到info.plist文件并将“查看基于控制器的状态栏外观”的值设置为“NO”。

答案 3 :(得分:0)

我遇到了类似的问题,但是当我从相机胶卷回到我的UIViewController时。我用推送UIImagePickerController的控制器中的跟随代码解决了这个问题:

首先我打开Picker

- (void)showPhotoPicker:(UIImagePickerControllerSourceType)sourceType
{
    [self.picker setSourceType:sourceType];
    [self presentViewController:self.picker animated:YES completion:nil];
}

我保持状态栏没有任何问题,我看到有些人有问题,我没有

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

在completition上调用视图需求布局非常重要

// Apply photo selected
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self.enterPicture setImage:info[UIImagePickerControllerEditedImage]];
    [picker dismissViewControllerAnimated:YES completion:^(void){[self.view setNeedsLayout];}];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^(void){[self.view  setNeedsLayout];}];
}

这就是魔术:

// fix the status bar & navigation bar after close the photo album on IOS7
-(void)viewWillLayoutSubviews
{
    UINavigationBar *navBar= [[self navigationController] navigationBar];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7 && navBar) {
        CGFloat statusBarHeight = [[UIApplication sharedApplication] isStatusBarHidden] ? 0.0f : 20.0f;
        [navBar setFrame:CGRectMake(0, statusBarHeight, navBar.frame.size.width, navBar.frame.size.height)];
        [self.view setBounds:CGRectMake(0, (statusBarHeight + navBar.frame.size.height) * -1, self.view.bounds.size.width, self.view.bounds.size.height)];
    }
}

答案 4 :(得分:0)

只需添加到控制器:

- (void)viewWillAppear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}