gpt4 book ai didi

ios - initWithFrame 被赋予空帧

转载 作者:行者123 更新时间:2023-11-29 03:50:41 26 4
gpt4 key购买 nike

我正在尝试使用 UINavigationController,它利用 UINavigationBar 的自定义子类。但是,我遇到了 initWithFrame 的问题。当调用 initWithFrame 时,我打印出尺寸,它显示 0.0 宽度和 0.0 高度。但是,当我在 UINavigationBar 子类内部的 -(void)layoutSubviews 中打印出相同的框架时,该框架已正确设置。为什么 initWithFrame 收到零大小的帧,我该怎么做才能解决这个问题?手动调用 initWithFrame 或类似的东西听起来不正确,因为 UINavigationController 应该处理这个问题。相关代码如下:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

UINavigationController *navController = [[UINavigationController alloc] initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:nil];
CoolViewController *vc = [[CoolViewController alloc] init];
[navController pushViewController:vc animated:YES];
self.window.rootViewController = navController;

[self.window makeKeyAndVisible];

return YES;
}

CustomNavigationBar.m

- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
NSLog(@"initWithFrame with frame: %f, %f", frame.size.width, frame.size.height); // This prints "initWithFrame with frame: 0, 0"
}

return self;
}

谢谢!

最佳答案

我相信导航 Controller 实际上并未将帧大小设置为指定值。相反,它会向导航栏询问其 sizeThatFits: 并使用它。根据定义,这是无法完成并传递给 initWithFrame: 的。因此,您想要使用 sizeThatFits:setFrame:layoutSubviews 的组合来添加、调整大小和重置导航栏内容。

关于ios - initWithFrame 被赋予空帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17117303/

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