截至 2012 年,在 iOS 4 上实现自定义导航栏样式的最佳方式是什么

发布于 2024-12-26 07:12:05 字数 754 浏览 4 评论 0原文

我知道两种方法,每种方法都有自己的缺点:

大家知道还有什么更好的方法吗? (同样,它适用于 iOS 4.0,因为我需要该应用程序与 iOS 4.0+ 兼容)

谢谢大家,

I know of two methods and each have their own disadvantages:

Do you guys know of any better method? (Again, it's for iOS 4.0 as I need the app to be compatible with iOS 4.0+)

Thanks guys,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听你说爱我 2025-01-02 07:12:05

您可能需要两种方法,具体取决于操作系统版本。这是因为在 5.0 及更高版本中,不会调用 drawRect 函数。相反,5.0 有自己的自定义导航栏例程。我最终使用了 4.0 和 5.0 方法,并在运行时确定版本并选择合适的版本。

至于第一种方法,我不明白为什么你真的需要界面生成器。只需以编程方式实例化您的对象并将它们添加为子视图即可。界面生成器本身并没有做更多的事情——它只是处理它们的一种更方便的方法。

You probably need two methods, conditioned by the OS version. This is because on 5.0 and later, the drawRect function isn't called. Instead, 5.0 has it's own routines for custom navigation bar. I ended up using both 4.0 and 5.0 methods, and determining the version and choosing the proper one at run time.

As far as the first method, I don't see why you really need interface builder. Just instantiate your objects programmatically and add them as subviews. Interface builder itself doesn't do much more than that -- it's just a handier way of dealing with them.

窝囊感情。 2025-01-02 07:12:05

实际上,不需要调配方法、添加类别或使用 Interface Builder,而是可以子类化 UINavigationBar 并使用 NSKeyedUnarchiver 更改 UINavigationController.navigationBar 的类 到您的自定义子类。

+ (UINavigationController*)customizableNavigationControllerWithController:(UIViewController*)controller {

NSAssert(controller != nil, @"UINavigationController(CustomNavigationBar) customizableControllerWithController: controller can not be nil");

UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:nil] autorelease];

NSData *navControllerData = [NSKeyedArchiver archivedDataWithRootObject:navController];

NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:navControllerData] autorelease];

[unarchiver setClass:[CustomNavigationBar class] forClassName:@"UINavigationBar"];

UINavigationController *customizableNavController = [unarchiver decodeObjectForKey:@"root"];

customizableNavController.viewControllers = [NSArray arrayWithObject:controller];

return customizableNavController;

}

Actually, there's no need to swizzle methods, add categories or use Interface Builder, instead, you can subclass UINavigationBar and use NSKeyedUnarchiver to change the class of UINavigationController.navigationBar to your custom subclass.

+ (UINavigationController*)customizableNavigationControllerWithController:(UIViewController*)controller {

NSAssert(controller != nil, @"UINavigationController(CustomNavigationBar) customizableControllerWithController: controller can not be nil");

UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:nil] autorelease];

NSData *navControllerData = [NSKeyedArchiver archivedDataWithRootObject:navController];

NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:navControllerData] autorelease];

[unarchiver setClass:[CustomNavigationBar class] forClassName:@"UINavigationBar"];

UINavigationController *customizableNavController = [unarchiver decodeObjectForKey:@"root"];

customizableNavController.viewControllers = [NSArray arrayWithObject:controller];

return customizableNavController;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文