Question

I have a viewController B class with xib file, and I add this viewController to the parent view A controller with this in my child.

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

What I want is, when the B viewController is displayed, hide the status Bar off the app.

If I use in the A Controller :

-(BOOL)preferStatusBarHidden {

return YES;

}

the status bar is hidden, but I want to do that only from the B controller but it's not working.

What can I do ?

Thanks.

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

View 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
Was it helpful?

Solution 2

Write below line inside DidLoad() method of ViewController A (where you want to show the status bar)

[[UIApplication sharedApplication] setStatusBarHidden:NO];

Write below line inside DidLoad() method of ViewController B (where you don't want to show the status bar)

[[UIApplication sharedApplication] setStatusBarHidden:YES];

Add below code in appDelegate.m file

- (BOOL)prefersStatusBarHidden {
      return YES;
}

OTHER TIPS

Your view controller acts as a custom container view controller, that encapsulates the underlying view controllers.

A is the custom container for B View Controllers. Since we are adding the child, we would be the one to tell the container from where to know whether to show or hide the status bar.

Hence for custom containers we need to override a method childViewControllerForStatusBarHidden. This method askes the container, for the view controller whose prefersStatusBarHidden is called to determine the state of status bar visibility.

Try this,

keep a BOOL to check B viewController is displayed or not. And in preferStatusBarHidden

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

and on adding 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];
}

If i put this in the parent view Controller, the status bar is hidden :

-(BOOL)preferStatusBarHidden {

 return YES ;

}

but in need also to do this in the child view controller and the code isn't called in the child view

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top