iOS从子视图控制器隐藏statusBar

时间:2014-01-29 10:27:44

标签: ios statusbar viewcontroller

我有一个带有xib文件的viewController B类,我将这个viewController添加到父视图中我的孩子中有一个控制器。

[Acontroller addChildViewController:self]; // self is B
[Acontroller.view addSubview:self.view];

我想要的是,当显示B viewController时,隐藏应用程序的状态栏。

如果我在A控制器中使用:

-(BOOL)preferStatusBarHidden {

return YES;

}

状态栏已隐藏,但我只想从B控制器执行此操作,但它无效。

我该怎么办?

感谢。

View Controller B :

//
//  AppehourInterstitielAds.m
//  AppTestSdk
//
//  Created by Administrateur on 22/01/2014.
//  Copyright (c) 2014 R. All rights reserved.
//

#import "AppehourInterstitielAds.h"
#import "AppehourSdk.h"

@interface AppehourInterstitielAds ()

@end

@implementation AppehourInterstitielAds

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id)initWithViewController:(UIViewController*)controller;{



    if (![AppehourSdk isNetworkConnected]){

        return nil;
    }


    self = [super init];
    vController = controller ;


    //------ Chargement des différents layouts selon taille écran

    // iPAD
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){

       self.view =  [[NSBundle mainBundle]loadNibNamed:@"interstitielAdsIPad" owner:self options:nil][0];
    }
    //iPhone
    else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
        CGSize size = [[UIScreen mainScreen]bounds].size;

        //iphone 3.5"
        if (size.height == 480){

            self.view =  [[NSBundle mainBundle]loadNibNamed:@"interstitielAdsIPhone3.5" owner:self options:nil][0];

        }
        //iphone 4"
        else {

            self.view =  [[NSBundle mainBundle]loadNibNamed:@"interstitielAdsIPhone4" owner:self options:nil][0];

        }   
    }

    //------------- Construction requête


    if ([[AppehourSdk getDeviceId]isEqualToString:NSLocalizedStringFromTable(@"device_debug_id", @"appehourValues", nil)]){

        NSLog(@"is debug device");
        isDebugDevice = @"true";
    }
    else {

        isDebugDevice = @"false" ;
    }

    frame = self.webViewInterst.frame ;

    width = [NSString stringWithFormat:@"%d", (int)frame.size.width];
    height = [NSString stringWithFormat:@"%d", (int)frame.size.height];

    NSLog(@"ads interstitiel width: %@", width ) ;
    NSLog(@"ads interstitiel height: %@", height ) ;


    NSError *error;

    NSMutableDictionary *dictionnaryDatas  = [[NSMutableDictionary alloc]init];
    NSMutableDictionary *dictionnaryDatasAds  = [[NSMutableDictionary alloc]init];


    [dictionnaryDatasAds setObject:width forKey:@"width"];
    [dictionnaryDatasAds setObject:height forKey:@"height"];
    [dictionnaryDatasAds setObject:@"interstitiel" forKey:@"type"];
    [dictionnaryDatasAds setObject:[AppehourSdk getDeviceId] forKey:@"device_id"];
    [dictionnaryDatasAds setObject:[AppehourSdk getAppId] forKey:@"app_id"];
    [dictionnaryDatasAds setObject:isDebugDevice forKey:@"is_debug_device"];


    // conversion en json
    NSData *datas = [NSJSONSerialization dataWithJSONObject:dictionnaryDatasAds options:0 error: &error];


    // url datas
    [dictionnaryDatas setObject:NSLocalizedStringFromTable(@"campaign_id", @"appehourValues", nil) forKey:@"cid"];
    [dictionnaryDatas setObject:@"ads" forKey:@"cat"];
    //ajout du json dans data
    [dictionnaryDatas setObject:datas forKey:@"data"];

    NSData *datasToSend = [NSKeyedArchiver archivedDataWithRootObject:dictionnaryDatas];

    NSURL *nsUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@", @"http://",TAG_DOMAIN,@"/scripts/cpi.php" ]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsUrl];


    [request setHTTPMethod:@"POST"];
    // ajout des datas a la requete
    [request setHTTPBody:datasToSend];

    // Create url connection and fire request
    NSURLConnection *connectionGetAds = [[NSURLConnection alloc] initWithRequest:request delegate:self];


    //--------------- End build request----------------------

   // [vController addChildViewController:self]; // ajout du childViewController au controller principal
    [vController addChildViewController:self];

    self.webViewInterst.scrollView.scrollEnabled = NO; // désactiver scroll dans webView

    self.webViewInterst.delegate = self ; // call the webView methods


    return nil;

}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[UIApplication sharedApplication]setStatusBarHidden:YES];



}


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

- (IBAction)buttonClose:(id)sender {


    [self.view removeFromSuperview];
    [self removeFromParentViewController];
    [timer invalidate];


}

-(void)decrement {

    if (seconds == 0){

        [self.view removeFromSuperview];
        [self removeFromParentViewController];
        [timer invalidate];

    }else {

        seconds--;
        _secondsRemain.text = [NSString stringWithFormat:@"%d%@", seconds, @" seconds remaining"];
        timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(decrement) userInfo:nil repeats:NO];

    }

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response {

    NSLog(@"response code interstitiel : %d", [response statusCode]);

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    //[responseDatas appendData:data] ;

}

-(void)connectionDidFinishLoading:(NSURLConnection*)connection {

    NSError *error;

    //dictAds = [NSJSONSerialization JSONObjectWithData:responseDatas options:NSJSONReadingMutableLeaves error:&error];

    //----- TEMPORAIRE

    NSMutableDictionary *tempDict = [[NSMutableDictionary alloc]init];

    [tempDict setObject:@"1245" forKey:@"id"];
    [tempDict setObject:@"https://associate.w3i.com/Images/integration/ios-NR-I-portrait.png" forKey:@"url"];
    [tempDict setObject:@"interstitiel" forKey:@"type"];
    [tempDict setObject:@"tok" forKey:@"token"];
    [tempDict setObject:@"clic" forKey:@"rem"];
    [tempDict setObject:@"10" forKey:@"valid_delay"];


    //----------

    NSURL *url = [NSURL URLWithString:[tempDict objectForKey:@"url"]];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    [self.webViewInterst loadRequest:requestObj];

    seconds = [[tempDict objectForKey:@"valid_delay"] intValue] ; // seconds before close ads

    [NSThread sleepForTimeInterval:3]; // wait before display the view

    [vController.view addSubview:self.view];

}

// webVew finis chargement
-(void)webViewDidFinishLoad:(UIWebView *)webView {



    [vController.view addSubview:self.view]; // ajoute la vue
    [self decrement];

    [self prefersStatusBarHidden];
    [vController prefersStatusBarHidden];

}

@end

查看A:

//
//  ViewController.m
//  AppTestSdk
//
//  Created by Administrateur on 09/01/2014.
//  Copyright (c) 2014 R. All rights reserved.
//

#import "ViewController.h"
#import "AppehourInterstitielAds.h"

@interface ViewController (){


    AppehourSdk* appehour;
    AppehourInterstitielAds *ads ;


}

@end

@implementation ViewController



-(void)didReceiveMemoryWarning
{
 [super didReceiveMemoryWarning];
  // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    [_appehourAds initAds:[AppehourSdk getDeviceId]];
    [[UIApplication sharedApplication]setStatusBarHidden:NO];

}

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

}

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

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

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 // Return YES for supported orientations
  return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  }

- (IBAction)button:(id)sender {

    /*
    UILocalNotification *notifcation = [[UILocalNotification alloc]init];
    notifcation.alertBody = @"Contenu";
    notifcation.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
*/

    ads = [[AppehourInterstitielAds alloc]initWithViewController:self];


    NSString *lemessage = [[NSString alloc] initWithFormat:@"Button clicked !"];
    _labelText.text = lemessage;
    NSLog(@"Button clicked");


    NSMutableDictionary *dictionnary = [[NSMutableDictionary alloc]init];
    [dictionnary setObject:@"value" forKey:@"key"];

    appehour = [[AppehourSdk alloc]init];

    [appehour trackEvent:@"Track" :dictionnary :@"8656"];


}


@end

4 个答案:

答案 0 :(得分:1)

您的视图控制器充当自定义容器视图控制器,它封装底层视图控制器。

A是B视图控制器的自定义容器。由于我们正在添加孩子,我们将告诉容器从哪里知道是否显示或隐藏状态栏。

因此,对于自定义容器,我们需要覆盖方法childViewControllerForStatusBarHidden。对于调用prefersStatusBarHidden的视图控制器来确定状态栏可见性的状态,此方法会检测容器。

答案 1 :(得分:0)

试试这个,

保持BOOL检查是否显示B viewController。并在preferStatusBarHidden

- (BOOL)prefersStatusBarHidden {
    if (bDisplayed) {
        return YES;
    }
    return NO;
}

并添加B viewController

bDisplayed = YES;
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
{
    // if iOS 7
    [self prefersStatusBarHidden];
    [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
} else {
    // if iOS 6
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}

答案 2 :(得分:0)

在ViewController A的DidLoad()方法中写下面的行(你要在哪里显示状态栏)

[[UIApplication sharedApplication] setStatusBarHidden:NO];

在ViewController B的DidLoad()方法中写下面的行(你不想显示状态栏)

[[UIApplication sharedApplication] setStatusBarHidden:YES];

在appDelegate.m文件中添加以下代码

- (BOOL)prefersStatusBarHidden {
      return YES;
}

答案 3 :(得分:0)

如果我把它放在父视图控制器中,状态栏将被隐藏:

-(BOOL)preferStatusBarHidden {

 return YES ;

}

但是还需要在子视图控制器中执行此操作,并且不在子视图中调用代码