2009-08-25 6 views
4

Je charge un écran de démarrage lorsque mon application démarre. Ensuite, je veux charger un TabBarController et c'est ViewControllers. Cependant, ma fenêtre TabBarController ne correspond pas à la taille de l'écran.Ajout d'un TabBarController en tant que sous-vue d'une vue

Probablement, les 3/4 du TabBar en bas se coupent et il y a un espace de près de 20 pixels en haut de l'écran en dessous de la barre d'état. Comment redimensionner le 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]; 
} 

Répondre

5

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); 
+0

Si vous exécutez self.tabBarController.view.frame = [[self view] frame] dans viewWillAppear cela fonctionnera sans que vous ayez à calculer vous-même les nombres, car à ce moment-là, les limites et le cadre ont été correctement/entièrement calculés. – pulkitsinghal

+0

serait-ce mieux répondre: tabBarController.view.frame = CGRectMake (0, 0, self.view.frame.size.width, self.view.frame.size.height); puisque la largeur et la hauteur varient en fonction de chaque appareil que l'utilisateur utilise? – lakesh

12

Je viens de terminer à peu près la même chose et j'ai rencontré les mêmes problèmes mais finalement je l'ai fait fonctionner.

  1. DEVENEZ Voir 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 
    
  2. Création d'une vue XIB appelé Test1View

  3. Ajouter un TabBarViewController au XIB

  4. Définissez le propriétaire du fichier dans le fichier XIB comme Test1ViewController.

  5. Connectez l'objet IBOutlet tbc du propriétaire du fichier au contrôleur de la barre d'onglets dans XIB.

  6. Connectez l'objet IBOutlet view du propriétaire du fichier à la vue dans XIB.

  7. Dans votre SplashViewController.h ajouter la propriété

    Test1ViewController *tabBarViewController; 
    
  8. Synthétiser l'tabBarViewController dans votre SplashViewController.m.

  9. Remplacez votre code de création TabBarController dans votre méthode loadView dans SplashViewController ce qui suit:

    tabBarViewController = [[Test1ViewController alloc] initWithNibName: 
         @"Test1View" bundle:[NSBundle mainBundle]]; 
    tabBarViewController.view.alpha = 0.0; 
    [self.view addSubview:[tabBarViewController view]]; 
    
  10. est ici le bit qui manquait pour moi.En Test1ViewController.m, vous devez ajouter la ligne suivante à la méthode viewDidLoad:

    self.view = tbc.view; 
    
  11. Enfin, je devais aussi changer la méthode finishedFading dans SplashViewController.m pour définir l'alpha à 1,0 sur la vue tabBarViewController.

    -(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.

+0

Merci, je cherchais quelque chose de similaire. Évalué la chose. –

+0

Merci! Je ne pouvais pas pour la vie de moi le faire fonctionner, s'avère qu'il me manquait l'étape 10. – link664

+0

Presque tout dans cette réponse était mort-sur autre que l'étape 10. Pour une raison qui ne fonctionne tout simplement pas maintenant, c'est peut-être un iOS5 chose? Au lieu de placer self.tabBarController.view.frame = [[self view] frame] dans viewWillAppear a fait l'affaire pour moi. – pulkitsinghal

Questions connexes