截至 2012 年,在 iOS 4 上实现自定义导航栏样式的最佳方式是什么
我知道两种方法,每种方法都有自己的缺点:
http://idevrecipes.com/2011/01/12/how-do-iphone-apps-instagramreederdailybooth-implement-custom-navigationbar-with-variable-width-back-buttons/ 这为您提供了最大的灵活性,您可以自定义导航栏上的所有内容。但是,您需要使用界面生成器来创建导航栏。如果您以编程方式创建导航栏,则不可能拥有自定义导航栏。如果我错了,请纠正我,我很想知道解决方案。
http://sebastiancelis.com/2009/12/21 /添加-背景-图像-uinavigationbar/ 这一切都是通过编程完成的,但是,您被迫显示导航栏标题,并且无法自定义按钮。
大家知道还有什么更好的方法吗? (同样,它适用于 iOS 4.0,因为我需要该应用程序与 iOS 4.0+ 兼容)
谢谢大家,
I know of two methods and each have their own disadvantages:
http://idevrecipes.com/2011/01/12/how-do-iphone-apps-instagramreederdailybooth-implement-custom-navigationbar-with-variable-width-back-buttons/
This one gives you the most flexibility, you can customize everything on the nav bar. However, you need to use the interface builder to create your nav bar. If you create your nav bar programmatically, it's not possible to have a custom nav bar. Please correct me if I'm wrong, i would love to know the solution.http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/
This one is done all programmatically, however, you are forced to display the nav bar title, and you can't customize the buttons.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要两种方法,具体取决于操作系统版本。这是因为在 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.
实际上,不需要调配方法、添加类别或使用 Interface Builder,而是可以子类化
UINavigationBar
并使用NSKeyedUnarchiver
更改UINavigationController.navigationBar 的类
到您的自定义子类。}
Actually, there's no need to swizzle methods, add categories or use Interface Builder, instead, you can subclass
UINavigationBar
and useNSKeyedUnarchiver
to change the class ofUINavigationController.navigationBar
to your custom subclass.}