I found this: Subclass UITabBarController to adjust its frame which explains how to achieve what I was aiming for in the answer. To paraphrase the post there, you have to register to be notified when the TabBarController's selected view changes, and then apply your geometry there. Otherwise it gets blown away by the TabBarController.
I am attempting to resize my UITabBarController to show a 20px high status message when certain tasks are ocurring. The curious thing is that if I step through the method which resizes the TabBarController, the origin of the frame changes.
When the view first appears, the UITabBarController's y origin is set to 0. Even though the applicatioinScreen y.origin is 20. If I resize when the app first loads, everything is off. The subviews do not resize. After this first resize, if I view a different tab, the TabBarController adjusts itself to the proper size, and subsequent calls to my resizing method confirm this.
This is how I am installing the UITabBarcontroller:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[AppController getGroupViewControllerArray] animated:NO];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
And this is how I am resizing the TabBarController:
-(void)resizeTabToShowActivity {
CGRect newFrame = mainTabBarController.view.frame;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
newFrame.origin.y = applicationFrame.origin.y + 20;
newFrame.size.height = applicationFrame.size.height - 20;
mainTabBarController.view.frame = newFrame;
}
I have noticed that if I resize directly after installing the TabBarController in the window, everything works fine. For debugging purposes, I've moved the resize method into the AppDelegate as well, and still no joy.
Thank you
I was able to resize a UITabBarController by containing it within a parent UIViewController, and making the parent UIViewController as the root view controller for the app.
Here's my parent UIViewController
// .h file
@interface MyTabBarViewController : UIViewController <UITabBarControllerDelegate>
@property(nonatomic, strong) IBOutlet UITabBarController *tbc;
@end
// .m file
@implementation MyTabBarViewController
@synthesize tbc=_tbc;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (_tbc == nil) {
CGRect frame = [[UIScreen mainScreen] bounds];
// arbitrary numbers, just to illustrate the point
CGRect smallFrame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width-300, frame.size.height-100);
_tbc = [[UITabBarController alloc] init];
_tbc.viewControllers = @[[[MyTabBarViewController1 alloc] init], [[MyTabBarViewController2 alloc] init]];
//_tbc.view.backgroundColor = [UIColor blueColor];
_tbc.view.autoresizesSubviews = YES;
_tbc.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_tbc.view.frame = smallFrame;
[self.view addSubview:_tbc.view];
}
}
MyViewController1 and MyViewController2 are just generic UIViewController subclasses that have a UILabel.
Here's my app delegate code that loads the parent UIViewController
@implementation MyTabBarAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MyTabBarViewController *vc = [[MyTabBarViewController alloc] init];
vc.view.backgroundColor = [UIColor yellowColor];
vc.definesPresentationContext = NO;
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With