添加 TabBarController 作为视图的子视图

发布于 2024-08-03 17:49:28 字数 2771 浏览 1 评论 0原文

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

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 screen size.

Probably 3/4 of the TabBar at the bottom is getting cut off and There is a slim aprox 20 pixel gap at the top of the screen below the status bar. How do I resize the TabBarController properly?

Here is the code in my SplashViewController loading the splash view, and the 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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晒暮凉 2024-08-10 17:49:28

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

  1. 在 Xcode 中创建一个名为 Test1ViewController 的视图控制器类并添加以下内容:

    @interface Test1ViewController : UIViewController {
        IBOutlet UITabBarController *tbc;
    }
    
    @property(非原子,保留)IBOutlet UITabBarController *tbc;
    @结尾
    
  2. 创建一个名为 Test1View 的视图 XIB

  3. 向 XIB 添加一个 TabBarViewController

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

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

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

  7. 在 SplashViewController.h 添加属性

    Test1ViewController *tabBarViewController;
    
  8. SplashViewController.m 中合成 tabBarViewController

  9. SplashViewControllerloadView 方法中的 TabBarController 创建代码替换为以下内容:

    tabBarViewController = [[Test1ViewController alloc] initWithNibName:
            @“Test1View”包:[NSBundle mainBundle]];
    tabBarViewController.view.alpha = 0.0;
    [self.view addSubview:[tabBarViewController 视图]];
    
  10. Here's the bit这对我来说是缺失的。在 Test1ViewController.m 中,您需要将以下行添加到 viewDidLoad 方法中:

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

    -(void) finishFading
    {
        [UIView beginAnimations:nil 上下文:nil];
        [UIView setAnimationDuration:0.75];
        self.view.alpha = 1.0;
        tabBarViewController.view.alpha = 1.0;
        [UIView提交动画];
        [splashImageView 从Superview 删除];
    }
    

我希望这有帮助。

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.

小镇女孩 2024-08-10 17:49:28

我终于找到了一些有用的东西。而不是:

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

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

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

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文