gpt4 book ai didi

iOS 对 subview Controller 隐藏状态栏

转载 作者:行者123 更新时间:2023-11-29 12:54:47 25 4
gpt4 key购买 nike

我有一个带有 xib 文件的 viewController B 类,我将这个 viewController 添加到我的 child 的父 View A Controller 中。

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

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

如果我在 A Controller 中使用:

-(BOOL)preferStatusBarHidden {

return YES;

}

状态栏是隐藏的,但我只想从 B Controller 执行此操作,但它不起作用。

我能做什么?

谢谢。

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

最佳答案

您的 View Controller 充当自定义容器 View Controller ,它封装了底层 View Controller 。

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

因此,对于自定义容器,我们需要重写方法 childViewControllerForStatusBarHidden。此方法向容器询问其 prefersStatusBarHidden 被调用以确定状态栏可见性状态的 View Controller 。

关于iOS 对 subview Controller 隐藏状态栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21428234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com