添加TabBarController作为视图的子视图

时间:2009-08-25 16:55:54

标签: cocoa-touch uitabbarcontroller frame bounds

当我的应用启动时,我正在加载启动画面。然后我想加载一个TabBarController和它的ViewControllers。但是,我的TabBarController窗口不会缩放到屏幕大小。

底部TabBar的3/4可能会被切断,状态栏下方屏幕顶部有一个细长的20像素间隙。如何正确调整TabBarController的大小?

这是加载启动视图的SplashViewController中的代码,以及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];
}

2 个答案:

答案 0 :(得分:12)

我刚刚完成了相同的问题并遇到了同样的问题,但最终让它运转​​了。

  1. 在Xcode中创建一个名为Test1ViewController的View Controller类,并添加以下内容:

    @interface Test1ViewController : UIViewController {
        IBOutlet UITabBarController *tbc;
    }
    
    @property (nonatomic,retain) IBOutlet UITabBarController *tbc;
    @end
    
  2. 创建名为Test1View

  3. 的视图XIB
  4. TabBarViewController添加到XIB

  5. 将XIB中的文件所有者设置为Test1ViewController

  6. 将文件所有者中的tbc IBOutlet连接到XIB中的标签栏控制器。

  7. 将文件所有者中的view IBOutlet连接到XIB中的视图。

  8. 在您的SplashViewController.h中添加属性

    Test1ViewController *tabBarViewController;
    
  9. 合成tabBarViewController中的SplashViewController.m

  10. 使用以下内容替换TabBarController loadView方法中的SplashViewController创建代码:

    tabBarViewController = [[Test1ViewController alloc] initWithNibName:
            @"Test1View" bundle:[NSBundle mainBundle]];
    tabBarViewController.view.alpha = 0.0;
    [self.view addSubview:[tabBarViewController view]];
    
  11. 这是我失踪的一点。在Test1ViewController.m中,您需要将以下行添加到viewDidLoad方法:

    self.view = tbc.view;
    
  12. 最后,我还必须更改finishedFading中的SplashViewController.m方法,将tabBarViewController视图上的alpha设置为1.0。

    -(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];
    }
    
  13. 我希望这会有所帮助。

答案 1 :(得分:5)

我终于找到了有效的方法。而不是:

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

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

因为我无法找到此大小的自动或命名设置,所以我必须创建自己的矩形,该大小是屏幕大小减去statusBar。

 tabBarController.view.frame = CGRectMake(0,0,320,460);