开发者

UITabBarController - Initial Frame and Resizing

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 installi开发者_如何学Cng 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;
}


The UITabBarController normally returns YES from wantsFullScreenLayout, which means that its view when used as the root view controller will be sized for the full screen including the area under the status bar. The controller then sizes its subviews to avoid the area under the status bar, as needed.

You can try setting wantsFullScreenLayout to NO, which should make it be sized to not be under the status bar as you seem to be expecting.


Try this on UITabBarController subclass:

- (void)viewDidLoad {
    [super viewDidLoad];

    // A UITabBarController's view has two subviews: the UITabBar and a container UITransitionView that is
    // used to hold the child views. Save a reference to the container.
    for (UIView *view in self.view.subviews) {
        if (![view isKindOfClass:[UITabBar class]]) {
            [view setFrame:RESIZED_FRAME];

        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜