将TabBarController添加为View的子视图

4
当我的应用程序启动时,我正在加载一个启动屏幕。然后,我想要加载一个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个回答

12

我刚刚完成了几乎相同的任务,并遇到了相同的问题,但最终我成功解决了。

在Xcode中创建一个名为Test1ViewController的视图控制器类,并添加以下内容:
``` @interface Test1ViewController : UIViewController { IBOutlet UITabBarController *tbc; }
@property (nonatomic,retain) IBOutlet UITabBarController *tbc; @end ```
创建名为Test1View的XIB文件。
在XIB中添加TabBarViewController
将XIB中的“文件所有者”设置为Test1ViewController
将文件所有者中的tbc IBOutlet与XIB中的选项卡栏控制器连接。
将文件所有者中的view IBOutlet与XIB中的视图连接。
SplashViewController.h中添加属性:
``` Test1ViewController *tabBarViewController; ```
SplashViewController.m中合成tabBarViewController
SplashViewControllerloadView方法中的TabBarController创建代码替换为以下代码:
``` tabBarViewController = [[Test1ViewController alloc] initWithNibName:@"Test1View" bundle:[NSBundle mainBundle]]; tabBarViewController.view.alpha = 0.0; [self.view addSubview:[tabBarViewController view]]; ```
Test1ViewController.m中,在viewDidLoad方法中添加以下行:
``` self.view = tbc.view; ```
最后,我还必须更改SplashViewController.m中的finishedFading方法,以将alpha设置为tabBarViewController视图中的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]; } ```
我希望这有所帮助。

谢谢,我正在寻找类似的东西。已经给它评分了。 - Wim Haanstra
谢谢!我真的无论如何都无法让它工作,结果发现我错过了第10步。 - dnatoli
除了第10步之外,这个答案几乎是完全正确的。由于某种原因,它现在不起作用了,也许是iOS5的问题?相反,在viewWillAppear中放置self.tabBarController.view.frame = [[self view] frame]对我有帮助。 - pulkitsinghal

5

我终于找到了一些有效的东西。相比之下:

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

或者

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

由于我找不到自动或命名的设置,以获取此尺寸,因此我必须创建自己的矩形,其大小为屏幕减去状态栏的大小。

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

如果您在viewWillAppear中运行self.tabBarController.view.frame = [[self view] frame],它将正常工作,而无需自己计算数字,因为此时边界和框架已经被正确/完全计算。 - pulkitsinghal
这个答案更好吗:tabBarController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 因为宽度和高度会根据用户使用的设备而变化? - lakshmen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接