UITabBarController - 初始帧和调整大小

UITabBarController - 初始帧和调整大小

问题描述:

我发现这个:Subclass UITabBarController to adjust its frame它解释了如何实现我在答案中的目标。为了解释那里的帖子,您必须注册才能在TabBarController的选定视图更改时得到通知,然后在那里应用您的几何体。否则它会被TabBarController吹走。UITabBarController - 初始帧和调整大小


我试图调整我的UITabBarController显示,当某些任务ocurring一个20像素高的状态消息。奇怪的是,如果我逐步调整TabBarController大小的方法,框架的原点就会改变。

当视图第一次出现时,UITabBarController的y原点设置为0.即使应用程序屏幕y.origin是20.如果我调整应用程序第一次加载时的大小,一切都将关闭。子视图不会调整大小。在第一次调整大小之后,如果我查看不同的选项卡,TabBarController将自己调整为适当的大小,随后对调整大小方法的调用将确认这一点。

这是怎么了安装的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; 
} 

这是怎么了调整的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; 

} 

我注意到,如果我在安装TabBarController后直接调整窗户,一切正常。出于调试的目的,我也调整了调整大小的方法到AppDelegate,并且仍然没有喜悦。

谢谢

+1

这个问题的解决是在这个问题的答案后发现我的应用程序的委托代码:http://stackoverflow.com/questions/1815059/subclass-uitabbarcontroller-to - 调整其框架 – kav 2011-03-18 03:07:06

我能够通过包含它的母体的UIViewController内,并且使母体的UIViewController作为该应用的根视图控制器调整大小的UITabBarController。

这是我父母的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和MyViewController2是具有一个UILabel只是普通的UIViewController子类。

下面是加载父的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; 
} 
+0

将标签栏(或任何控制器)视图手动添加到视图中并不是一个好主意。控制器不会自动保留/释放,并且不会收到正常的视图控制器通知(例如旋转事件)。 – Rivera 2014-07-30 08:03:26

的的UITabBarController正常返回YES从wantsFullScreenLayout,这意味着作为根视图控制器使用时,其视图将尺寸适合全屏包括状态栏下的面积。然后控制器根据需要调整其子视图的大小以避免状态栏下的区域。

您可以尝试将wantsFullScreenLayout设置为NO,这应该使其大小不会像您期望的那样位于状态栏的下方。

+0

感谢您的快速回复,但这并不是诀窍。我有一个TabBarController作为rootviewcontroller(我曾尝试添加一个子视图,并将其添加为rootviewcontroller)。这些选项卡是用三个自定义的UIViewController类创建的,这些类使用通用的UINavigationController类进行包装。我正在设置wantsFullScreenLayout = NO,但TabBarController仍在抓取整个屏幕。 – kav 2011-03-08 05:24:02

+0

@kav当我尝试使用这种方法时,发生了同样的事情。 – robhasacamera 2012-07-27 15:28:57

尝试此上的UITabBarController亚类:

- (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

我会的,谢谢。对不起,迟到的回复,直到今天我都没有看到。 – kav 2011-11-02 23:23:52