子类UITabBarController来调整其框架 [英] Subclass UITabBarController to adjust its frame

查看:88
本文介绍了子类UITabBarController来调整其框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题调整UITabBarController的大小,因为我只想要占据屏幕的下半部分。它似乎强制自己显示屏幕的整个高度(减去状态栏,如果显示)。



我已经尝试将其子类化并修改控制器的视图框架上的方法,如 viewWillAppear; ,但它只是' t工作。在某些情况下,我可以强制它的位置被改变,但一旦选择了另一个选项卡,或者如果另一个视图控制器被模态地呈现在它的前面,它会调整回到全屏!



我已经尝试设置UIWindow(添加标签栏控制器)到 autoresizesSubviews:NO 和标签栏的 autoresizingMask:UIViewAutoresizingNone 但是不行!



任何帮助将非常感激!

$ b>我已经尝试过这个,并确定tabBarController会自动调整其视图,并撤消所有更改,当发生以下任何情况:




  • selectedViewController的值发生变化;

  • 界面方向更改;




我可以强制UITabBar的视图保持不变的大小注册所有上述事件,然后调用自定义调整大小方法。



您可以使用KVO观察selectedViewController的更改,如下所示:

  [tabBarController addObserver:self forKeyPath:@selectedViewControlleroptions:NSKeyValueObservingOptionNew context:NULL] 
pre>

观察对接口方向的更改可能有点棘手。如果你是从你的应用程序委托,或另一个专用于这个目的的对象,你可能没有得到标准UIViewController回调的界面方向更改。有两种方法来解决这个问题:(1)UIDevice和通知,和(2)让你的一个视图控制器生成一个回调。就个人而言,我建议后者,因为UIDevice通知往往是几个微秒不同步的接口,结果可能看起来锯齿。简化的实现:

  //在某个标题中(Prefix.pch工作)。 

extern NSString * const kOrinetationNotification;
extern NSString * const kOrinetationNotificationOrientationKey;

//在UIViewController的实例中

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[[NSNotificationCenter defaultCenter] postNotificationName :kOrientationNotification对象:self userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:toInterfaceOrientation] forKey:kOrientationNotificationOrientationKey]];
//在你的应用程序委托(或其他专用对象)init方法中执行

[[NSNotificationCenter defaultCenter] addObserver :self selector:@selector(orientationDidChange :) name:kOrientationNotification object:nil];

这应该确保你每次在标签栏的几何形状触发更改时收到这些回调后,您可以从每个回调中调用自己的自定义方法,并根据需要调整标签栏大小,而无需对其进行子类化。


I'm having some problems resizing a UITabBarController as I only want it to take up the bottom half of the screen. It seems to force itself to display the full height of the screen (minus the status bar if showing).

I have tried subclassing it and modifying the controller's view's frame on methods such as viewWillAppear; but it's just doesn't work. There are occasions where I can force it's position to be altered, but as soon as another tab is selected, or if another view controller is presented modally in front of it, it resizes back to full screen!

I've tried setting the UIWindow (which the tab bar controller is added to) to autoresizesSubviews:NO and the tab bar's autoresizingMask:UIViewAutoresizingNone but that doesn't do it!

Any help would be greatly appreciated!

解决方案

I've experimented with this and determined that the tabBarController will automatically resize its view and undo all your changes when any of the following happens:

  • value of selectedViewController changes;
  • interface orientation changes;
  • the tab bar is (re)loaded and placed inside a view/window.

I was able to force the UITabBar's view to remain of constant size by registering for all of the above events and then calling a custom resizing method from them.

You can observe changes to the selectedViewController with KVO, like so:

[tabBarController addObserver:self forKeyPath:@"selectedViewController" options:NSKeyValueObservingOptionNew context:NULL]

Observing changes to the interface orientation can be a bit tricky. If you're doing this from your application delegate, or another object dedicated to this purpose, chances are you're not getting the standard UIViewController callbacks for interfare orientation changes. There are two ways to work around this: (1) UIDevice and notifications, and (2) getting one of your view controllers to generate a callback. Personally, I would recommend the latter, as the UIDevice notifications tend to be a few microseconds out of sync with the interface, and the result can look jaggy. Simplified implementation:

// In a header somewhere (Prefix.pch works).

extern NSString *const kOrinetationNotification;
extern NSString *const kOrinetationNotificationOrientationKey;

// in an instance of UIViewController

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  [[NSNotificationCenter defaultCenter] postNotificationName:kOrientationNotification object:self userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:toInterfaceOrientation] forKey:kOrientationNotificationOrientationKey]];
  // do other stuff
}

// in your application delegate's (or other dedicated object) init method

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:kOrientationNotification object:nil];

This should make sure you get heads up every time something triggers a change in your tab bar's geometry. Once you're receiving these callbacks, you can call your own custom method from each of them and resize the tab bar as you see fit with no need to subclass it.

这篇关于子类UITabBarController来调整其框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆