Cocoa touch 添加TabBarController作为视图的子视图

Cocoa touch 添加TabBarController作为视图的子视图,cocoa-touch,uitabbarcontroller,frame,bounds,Cocoa Touch,Uitabbarcontroller,Frame,Bounds,当我的应用程序启动时,我正在加载启动屏幕。然后我想加载一个TabBarController和它的ViewControllers。但是,我的TabBarController窗口无法缩放到屏幕大小 底部的选项卡栏可能有3/4被切断,状态栏下方的屏幕顶部有一个约20像素的小间隙。如何正确调整TabBarController的大小 以下是我的SplashViewController中加载splash视图和TabBarController的代码: -(void)loadView{ // Init the

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

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

以下是我的SplashViewController中加载splash视图和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];
}

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

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

因为我找不到此大小的自动或命名设置,所以我必须创建自己的矩形,即屏幕大小减去状态栏

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

我刚刚完成了几乎相同的工作,遇到了相同的问题,但最终我还是成功了

  • 在Xcode中创建名为
    Test1ViewController
    的视图控制器类,并添加以下内容:

    @interface Test1ViewController : UIViewController {
        IBOutlet UITabBarController *tbc;
    }
    
    @property (nonatomic,retain) IBOutlet UITabBarController *tbc;
    @end
    
    tabBarViewController = [[Test1ViewController alloc] initWithNibName:
            @"Test1View" bundle:[NSBundle mainBundle]];
    tabBarViewController.view.alpha = 0.0;
    [self.view addSubview:[tabBarViewController view]];
    
  • 创建一个名为
    Test1View

  • 在XIB中添加一个
    TabBarViewController

  • 在XIB中将文件的所有者设置为
    Test1ViewController

  • 将文件所有者中的
    tbc
    IBOutlet连接到XIB中的选项卡栏控制器

  • 将文件所有者中的
    视图
    IBOutlet连接到XIB中的视图

  • 在SplashViewController.h中添加属性

    Test1ViewController *tabBarViewController;
    
  • SplashViewController.m
    中合成
    选项卡BarViewController

  • SplashViewController
    中的
    loadView
    方法中的
    TabBarController
    创建代码替换为以下代码:

    @interface Test1ViewController : UIViewController {
        IBOutlet UITabBarController *tbc;
    }
    
    @property (nonatomic,retain) IBOutlet UITabBarController *tbc;
    @end
    
    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
    方法,以便在
    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];
    }
    

  • 我希望这能有所帮助。

    谢谢,我在找类似的东西。对这件事评价很高,谢谢!我一辈子都无法让它工作,结果我错过了第10步。除了第10步,这个答案中几乎所有的东西都死掉了。出于某种原因,现在这根本不起作用,也许这是一个失败的事情?相反,将self.tabBarController.view.frame=[[self-view]frame]放在ViewWillDisplay中对我起了作用。如果运行self.tabBarController.view.frame=[[self-view]frame]在view中,它似乎可以工作,而无需您自己计算出数字,因为此时边界和帧已正确/完全计算。这是更好的答案:tabBarController.view.frame=CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);由于宽度和高度根据用户使用的每个设备而变化?