Ajout d'un TabBarController comme sous-vue d'une vue
-
19-09-2019 - |
Question
Je suis le chargement d'un écran de démarrage lorsque mon application démarre. Ensuite, je veux charger un TabBarController et il est ViewControllers. Cependant, ma fenêtre TabBarController n'échelle pas à la taille de l'écran.
Probablement 3/4 du TabBar au fond devient coupé et il y a un écart mince de pixel aprox 20 en haut de l'écran sous la barre d'état. Comment puis-je changer la taille de la TabBarController correctement?
Voici le code dans mon SplashViewController chargement de la vue de démarrage, et le TabBarController:
-(void)loadView{
// Init the view
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
UIView *view = [[UIView alloc] initWithFrame:appFrame];
view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.view = view;
[view release];
splashImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Splash.png"]];
splashImageView.frame = CGRectMake(0,0,320,458);
[self.view addSubview:splashImageView];
viewController = [[FlashCardViewController alloc] initWithNibName:@"FlashCardViewController" bundle:[NSBundle mainBundle]];
//viewController.view.bounds = [[UIScreen mainScreen]bounds];
viewController.title = @"Quiz";
viewController.tabBarItem.image = [UIImage imageNamed:@"puzzle.png"];
UIViewController *viewController2 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
viewController2.title = @"Nada";
viewController2.tabBarItem.image = [UIImage imageNamed:@"magnifying-glass.png"];
//viewController.view.alpha = 0.0;
//[self.view addSubview:viewController.view];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:viewController, viewController2, nil];
[viewController2 release];
tabBarController.view.alpha = 0.0;
//tabBarController.tabBarItem.image = [UIImage imageNamed:@"State_California.png"];
//tabBarController.tabBarItem.title = @"State_California.png";
tabBarController.view.bounds = [[self view] bounds];
//tabBarController.view.frame = [[UIScreen mainScreen] applicationFrame];
[self.view addSubview:tabBarController.view];
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(fadeScreen) userInfo:nil repeats:NO];
}
-(void) fadeScreen
{
[UIView beginAnimations:nil context:nil]; // begin animation block
[UIView setAnimationDuration:0.75]; // sets animation duration
[UIView setAnimationDelegate:self]; // sets the delegate for this block
[UIView setAnimationDidStopSelector:@selector(finishedFading)]; // Calls finishFading
self.view.alpha = 0.0; // // Fades the alpha to 0 over animation
[UIView commitAnimations]; // Commits this block, done
}
-(void) finishedFading
{
[UIView beginAnimations:nil context:nil]; // Begin animation block
[UIView setAnimationDuration:0.75]; // set duration
self.view.alpha = 1.0; // fades the view to 1.0 alpha over .75 seconds
//viewController.view.alpha = 1.0;
tabBarController.view.alpha = 1.0;
[UIView commitAnimations];
[splashImageView removeFromSuperview];
}
La solution 2
J'ai finalement trouvé someting qui fonctionne. Au lieu de:
tabBarController.view.frame = [[UIScreen mainScreen] applicationFrame];
ou
tabBarController.view.bounds = [[self view] bounds];
Parce que je ne pouvais pas trouver et réglages automatiques ou nommés pour cette taille, je devais créer mon propre rectangle qui est la taille de l'écran moins la barre d'état.
tabBarController.view.frame = CGRectMake(0,0,320,460);
Autres conseils
Je viens de terminer à peu près la même et a couru dans les mêmes problèmes, mais finalement je l'ai eu de travail.
-
Créer une vue classe contrôleur dans Xcode appelé
Test1ViewController
et ajouter ce qui suit:@interface Test1ViewController : UIViewController { IBOutlet UITabBarController *tbc; } @property (nonatomic,retain) IBOutlet UITabBarController *tbc; @end
-
Création d'une vue
Test1View
appelé XIB -
Ajouter un
TabBarViewController
au XIB -
Définir le propriétaire dans le XIB être le
Test1ViewController
. du fichier
-
Connectez le
tbc
IBOutlet dans propriétaire au contrôleur barre d'onglets du fichier dans le XIB. -
Connectez le
view
IBOutlet dans le propriétaire du fichier à la vue dans le XIB. -
Dans votre SplashViewController.h ajouter la propriété
Test1ViewController *tabBarViewController;
-
Synthétiser la
tabBarViewController
dans votreSplashViewController.m
. -
Remplacez votre code de création de
TabBarController
dans votre méthode deloadView
enSplashViewController
ce qui suit:tabBarViewController = [[Test1ViewController alloc] initWithNibName: @"Test1View" bundle:[NSBundle mainBundle]]; tabBarViewController.view.alpha = 0.0; [self.view addSubview:[tabBarViewController view]];
-
Voici le bit qui manquait pour moi. En
Test1ViewController.m
, vous devez ajouter la ligne suivante à la méthodeviewDidLoad
:self.view = tbc.view;
-
Enfin, je dû également modifier la méthode de
finishedFading
enSplashViewController.m
pour définir l'alpha à 1,0 sur la vue detabBarViewController
.-(void) finishedFading { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.75]; self.view.alpha = 1.0; tabBarViewController.view.alpha = 1.0; [UIView commitAnimations]; [splashImageView removeFromSuperview]; }
J'espère que cette aide.