【问题标题】:iOS hides statusBar from child View ControlleriOS从子视图控制器隐藏状态栏
【发布时间】:2014-02-21 02:06:41
【问题描述】:

我有一个带有 xib 文件的 viewController B 类,我将此 viewController 添加到父视图 A 控制器中,并在我的孩子中使用它。

[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

【问题讨论】:

标签: ios statusbar viewcontroller


【解决方案1】:

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

A 是 B 视图控制器的自定义容器。由于我们要添加子项,因此我们会告诉容器从哪里知道是显示还是隐藏状态栏。

因此,对于自定义容器,我们需要重写方法 childViewControllerForStatusBarHidden。该方法向容器询问调用prefersStatusBarHidden 的视图控制器以确定状态栏可见性的状态。

【讨论】:

    【解决方案2】:

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

    -(BOOL)preferStatusBarHidden {
    
     return YES ;
    
    }
    

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

    【讨论】:

      【解决方案3】:

      试试这个,

      保留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];
      }
      

      【讨论】:

      • 我不能这样做,因为 B 是一个供其他开发人员使用的库,所以我不能告诉他们更改 plist
      【解决方案4】:

      在 ViewController A 的 DidLoad() 方法中写下一行(你想显示状态栏的地方)

      [[UIApplication sharedApplication] setStatusBarHidden:NO];
      

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

      [[UIApplication sharedApplication] setStatusBarHidden:YES];
      

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

      - (BOOL)prefersStatusBarHidden {
            return YES;
      }
      

      【讨论】:

      • 对那个 Flofloaud1034 感到抱歉。它没有躲避任何视线吗?请尝试在 viewWillAppear() 方法中编写此代码。我现在已经检查过了,它对我有用。
      • 我已经这样做了,但是当 ViewCONtrollerB 的内容出现时会显示状态栏
      • 你是在ios7还是ios6上运行?
      猜你喜欢
      • 1970-01-01
      • 2013-01-11
      • 2017-06-23
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多