我发现了这个:Subclass UITabBarController to adjust its frame,它解释了如何在答案中实现我的目标。要解释那里的帖子,您必须注册以在TabBarController的选定视图更改时收到通知,然后在那里应用您的几何。否则它会被TabBarController吹走。
我正在尝试调整我的UITabBarController的大小,以便在某些任务发生时显示20px高状态消息。奇怪的是,如果我单步执行调整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后直接调整大小,一切正常。出于调试目的,我已经将resize方法移动到AppDelegate中,但仍然没有乐趣。
谢谢
答案 0 :(得分:1)
我能够通过将UITabBarController包含在父UIViewController中来调整其大小,并将父UIViewController作为应用程序的根视图控制器。
这是我的父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子类。
这是我的app委托代码,用于加载父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;
}
答案 1 :(得分:0)
UITabBarController通常从wantsFullScreenLayout
返回YES,这意味着当用作根视图控制器时,其视图的大小将适用于整个屏幕,包括状态栏下的区域。然后控制器根据需要调整其子视图的大小以避开状态栏下的区域。
您可以尝试将wantsFullScreenLayout
设置为NO,这样可以使其大小不会像您期望的那样位于状态栏下。
答案 2 :(得分:0)
在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];
}
}
}