当我向导航栏添加图像时,为什么会出现左/右间隙?

发布于 2024-08-22 23:50:33 字数 347 浏览 17 评论 0原文

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 技术交流群。

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

发布评论

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

评论(1

无所的.畏惧 2024-08-29 23:50:33

这……可能不是您想要的方式——这不是 titleView 的用途。自定义导航栏背景的常用方法是在 UINavigationBar 上创建一个类别,如下所示:

 @implementation UINavigationBar(MyCustomBackground)
 - (void)drawRect:(CGRect)r
 {
      [[UIImage imageNamed:@"my-navigation-background.png"] drawInRect:self.bounds];
 }
 @end

将其放入项目中的 .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 on UINavigationBar, like so:

 @implementation UINavigationBar(MyCustomBackground)
 - (void)drawRect:(CGRect)r
 {
      [[UIImage imageNamed:@"my-navigation-background.png"] drawInRect:self.bounds];
 }
 @end

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.

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