Adding a TabBarController as the Subview of a View

后端 未结 2 445
抹茶落季
抹茶落季 2021-02-01 11:35

I am loading a splash screen when my app starts. Then I want to load a TabBarController and it\'s ViewControllers. However, my TabBarController window does not scale to the

相关标签:
2条回答
  • 2021-02-01 11:55

    I've just completed pretty much the same and ran into the same problems but eventually I got it working.

    1. Create a View Controller class in Xcode called Test1ViewController and add the following:

      @interface Test1ViewController : UIViewController {
          IBOutlet UITabBarController *tbc;
      }
      
      @property (nonatomic,retain) IBOutlet UITabBarController *tbc;
      @end
      
    2. Create a View XIB called Test1View

    3. Add a TabBarViewController to the XIB

    4. Set the File's Owner in the XIB to be the Test1ViewController.

    5. Connect the tbc IBOutlet in the File's Owner to the Tab Bar Controller in the XIB.

    6. Connect the view IBOutlet in the File's Owner to the View in the XIB.

    7. In your SplashViewController.h add the property

      Test1ViewController *tabBarViewController;
      
    8. Synthesize the tabBarViewController in your SplashViewController.m.

    9. Replace your TabBarController creation code in your loadView method in SplashViewController with the following:

      tabBarViewController = [[Test1ViewController alloc] initWithNibName:
              @"Test1View" bundle:[NSBundle mainBundle]];
      tabBarViewController.view.alpha = 0.0;
      [self.view addSubview:[tabBarViewController view]];
      
    10. Here's the bit that was missing for me. In Test1ViewController.m, you need to add the following line to the viewDidLoad method:

      self.view = tbc.view;
      
    11. Finally, I also had to change the finishedFading method in SplashViewController.m to set the alpha to 1.0 on the tabBarViewController view.

      -(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];
      }
      

    I hope this helps.

    0 讨论(0)
  • 2021-02-01 12:04

    I finally found someting that works. Instead of:

    tabBarController.view.frame = [[UIScreen mainScreen] applicationFrame];
    

    or

    tabBarController.view.bounds = [[self view] bounds];
    

    Because I couldn't find and automatic or named settings for this size, I had to create my own rectangle that is the size of the screen minus the statusBar.

     tabBarController.view.frame = CGRectMake(0,0,320,460);
    
    0 讨论(0)
提交回复
热议问题