gpt4 book ai didi

iphone - 子类 UITabBarController 来调整其框架

转载 作者:行者123 更新时间:2023-12-03 19:43:01 26 4
gpt4 key购买 nike

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

我尝试过对其进行子类化并在诸如 viewWillAppear; 之类的方法上修改 Controller 的 View 框架,但它不起作用。有时我可以强制改变它的位置,但是一旦选择另一个选项卡,或者如果另一个 View Controller 以模态方式呈现在它前面,它就会调整大小回到全屏!

我尝试将 UIWindow(添加了选项卡栏 Controller )设置为 autoresizesSubviews:NO 并将选项卡栏的 autoresizingMask:UIViewAutoresizingNone 设置为 autoresizingSubviews:NO 但事实并非如此做吧!

任何帮助将不胜感激!

最佳答案

我对此进行了实验,并确定当发生以下任何情况时,tabBarController 将自动调整其 View 大小并撤消所有更改:

  • selectedViewController 的值发生变化;
  • 界面方向发生变化;
  • 标签栏被(重新)加载并放置在 View /窗口内。

通过注册所有上述事件,然后从中调用自定义调整大小方法,我能够强制 UITabBar 的 View 保持恒定大小。

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

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

观察界面方向的变化可能有点棘手。如果您从应用程序委托(delegate)或专用于此目的的另一个对象执行此操作,则您很可能无法获得用于界面方向更改的标准 UIViewController 回调。有两种方法可以解决此问题:(1) UIDevice 和通知,以及 (2) 让 View Controller 之一生成回调。就我个人而言,我会推荐后者,因为 UIDevice 通知往往与界面不同步几微秒,并且结果可能看起来参差不齐。简化的实现:

// 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];

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

关于iphone - 子类 UITabBarController 来调整其框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1815059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com