我可以绘制 UINavigationBar 的背景作为自定义视图的背景吗?
我有一个自定义视图,我希望它看起来像 UINavigationBar。有没有办法让我像 UINavigationBar 一样绘制背景?
我不想绘制看起来像 UINavigationBar 的图像或渐变填充 - 我想使用与 UINavigationBar 相同的库代码(如果它是公共的)来绘制其背景。
I have a custom view which I would like to look like a UINavigationBar. Is there a way for me to draw the background the same way a UINavigationBar would?
I don't want to draw an image or a gradient fill that looks like a UINavigationBar - I want to use the same library code (if it is public) that a UINavigationBar does to draw its background.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UINavigationBar
的drawRect:
方法是私有的 - 您不能使用它来绘制您自己的视图的背景。最简单的方法是拍摄屏幕截图,将图像修剪为导航栏大小,添加到您的项目中,然后在视图的
drawRect:
方法中绘制图像,或添加 < code>UIImageView 作为视图的子视图。或者,是否可以使用
UIToolbar
而不是您的自定义视图?The
drawRect:
method of aUINavigationBar
is private - you can't use it to draw the background of your own view.The easiest way round this would be to take a screen shot, trim the image to nav bar size, add to your project, and then either draw the image in your view's
drawRect:
method, or add aUIImageView
as a subview of your view.Alternatively, would it be possible to use a
UIToolbar
instead of your custom view?您可以设置视图背景图像,并仅使用导航栏看起来已填充的图像。
就利用导航栏用于创建自身的任何内容而言,除非您只是在应用程序中构建导航栏,否则您可能会运气不好。否则,您最终将不得不在应用程序顶部构建自定义视图,手动绘制所有内容。那时,您最好实施导航栏。
You could set your view background image and just use an image that has the navigation bar look already filled in.
As far as tapping into whatever the navBar uses to create itself, you are probably out of luck unless you just build a navBar into your app. Otherwise you would end up having to build a custom view at the top of your app, draw everything in manually. At that point, you are probably better off implementing the navBar.