当我向导航栏添加图像时,为什么会出现左/右间隙?
UIImageView *navBarImageView = [[UIImageView alloc] initWithImage:navBarImage];
[navBarImageView setFrame:CGRectMake(0, 0, 320, 44)];
self.navigationItem.titleView = navBarImageView;
[navBarImageView release];
我正在尝试将图像添加到导航栏中,但是当我使用上面列出的代码时,它将图像放置到导航栏中,但在图像的左侧和右侧留下了灰色间隙。该图像是以 320x44 创建的,尽管我正在设置框架,但我不确定为什么要调整它的大小。
UIImageView *navBarImageView = [[UIImageView alloc] initWithImage:navBarImage];
[navBarImageView setFrame:CGRectMake(0, 0, 320, 44)];
self.navigationItem.titleView = navBarImageView;
[navBarImageView release];
I am trying to add an image to my navigationBar, but when I used the code as listed above, it places the image into the navigationBar but leaves a grey gap to the left and right of the image. The image was created at 320x44, I'm not sure why it is resizing it despite the fact that I am setting the frame.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这……可能不是您想要的方式——这不是
titleView
的用途。自定义导航栏背景的常用方法是在UINavigationBar
上创建一个类别,如下所示:将其放入项目中的 .m 文件中,您应该会看到所有导航栏都使用“ my-navigation-background.png”作为他们的背景。
That's... probably not how you want to do that—it's not what the
titleView
is for. The usual way to do a custom navigation-bar background is to create a category onUINavigationBar
, like so:Throw that in a .m file in your project and you should see all of your navigation bars use "my-navigation-background.png" as their background.