Hinzufügen eines TabBarController als Subview einer Ansicht
-
19-09-2019 - |
Frage
Ich lade einen Splash-Screen, wenn mein app beginnt. Dann mag ich einen TabBarController laden und es ist Viewcontrollers. Allerdings skaliert mein TabBarController Fenster auf die Bildschirmgröße nicht.
Wahrscheinlich 3/4 der TabBar am Boden wird immer abgeschnitten und es ist eine schlanke ca. 20 Pixel Lücke am oberen Rand des Bildschirms unterhalb der Statusleiste. Wie ändere ich die Größe der TabBarController richtig?
Hier ist der Code in meinem SplashViewController Laden der Splash-Ansicht und die 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];
}
Lösung 2
Schließlich fand ich, dass die Werke some. Statt:
tabBarController.view.frame = [[UIScreen mainScreen] applicationFrame];
oder
tabBarController.view.bounds = [[self view] bounds];
Weil ich nicht und automatische oder benannte Einstellungen für diese Größe finden konnte, musste ich mein eigenes Rechteck erstellen, die die Größe des Bildschirms minus der statusBar.
tabBarController.view.frame = CGRectMake(0,0,320,460);
Andere Tipps
Ich habe gerade so ziemlich die gleiche und läuft in die gleichen Probleme erfolgreich abgeschlossen, aber schließlich habe ich es zu arbeiten.
-
Erstellen Sie eine View-Controller-Klasse in Xcode genannt
Test1ViewController
und fügen Sie die folgende:@interface Test1ViewController : UIViewController { IBOutlet UITabBarController *tbc; } @property (nonatomic,retain) IBOutlet UITabBarController *tbc; @end
-
Erstellen Sie eine Ansicht XIB genannt
Test1View
-
Fügen Sie eine
TabBarViewController
zum XIB -
Stellen Sie den Besitzer der Datei in der XIB der
Test1ViewController
sein. -
Schließen Sie die
tbc
IBOutlet in der Betrieb Datei auf den Tab Bar-Controller in der XIB. -
Schließen Sie den
view
IBOutlet in der Betriebs Datei auf die Ansicht in der XIB. -
In Ihrem SplashViewController.h die Eigenschaft hinzufügen
Test1ViewController *tabBarViewController;
-
Synthesize die
tabBarViewController
in IhremSplashViewController.m
. -
Ersetzen Sie Ihren
TabBarController
Erstellungscode in IhrerloadView
Methode inSplashViewController
mit dem folgenden:tabBarViewController = [[Test1ViewController alloc] initWithNibName: @"Test1View" bundle:[NSBundle mainBundle]]; tabBarViewController.view.alpha = 0.0; [self.view addSubview:[tabBarViewController view]];
-
Hier ist das Bit, das für mich fehlte. In
Test1ViewController.m
, müssen Sie die folgende Zeile zurviewDidLoad
Methode hinzufügen:self.view = tbc.view;
-
Schließlich musste ich auch die
finishedFading
Methode inSplashViewController.m
verändert das Alpha bis 1,0 auf dertabBarViewController
Ansicht eingestellt werden.-(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]; }
Ich hoffe, das hilft.