在我的 UINavigationBar 中为我的标题添加图像而不是标题

Posted

技术标签:

【中文标题】在我的 UINavigationBar 中为我的标题添加图像而不是标题【英文标题】:Add an image instead of a title to my title in my UINavigationBar 【发布时间】:2013-09-25 07:38:11 【问题描述】:

我的 uiviewcontroller 中有以下代码嵌入在 uinavigationcontroller 中。

在 viewDidLoad 中:

[self addLogo];

下面:

- (void) addLogo

    UIImage *image = [UIImage imageNamed:@"logoblack.png"];
    UIImageView *myImageView = [[UIImageView alloc] initWithImage:image];

    myImageView.frame = CGRectMake(0, 0, 30, 30);
    myImageView.layer.cornerRadius = 5.0;
    myImageView.layer.masksToBounds = YES;
    myImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    myImageView.layer.borderWidth = 0.1;

   self.navigationItem.titleView = myImageView;

我拥有的图像是 434 x 434,但我希望它是 30 x 30。相反,它被拉伸并超过了导航栏中的

【问题讨论】:

为什么不在预览应用程序中缩放和调整图像大小并添加 30X30 帧 - 不要认为它很复杂 是的,我会这样做,只是想了解更多有关 ios api 的信息 你有没有尝试改变titleView的contentMode。对 UIViewContentModeBottom 说。 使用它来调整 [***.com/questions/2658738/… 【参考方案1】:

使用

myImageView.contentMode = UIViewContentModeScaleAspectFill;

这样图像视图就不会被拉伸,并保持它的纵横比

【讨论】:

以上是关于在我的 UINavigationBar 中为我的标题添加图像而不是标题的主要内容,如果未能解决你的问题,请参考以下文章

如何在我的 iphone 中为我的网站添加有效且受信任的安全证书?

为啥“npm install”在我的“package-lock.json”文件中为我的包添加“node_modules”前缀?

如何在我的蛇游戏中为我的蛇的身体部位使用不同的图像? (Python、Pygame、Snake)

如何在我的 Docker 自托管 Jitsi 服务器中为我的 Android 应用程序实现 jwt 令牌韵律插件?

如何在我的可视 C# Web 服务中为我的 android 应用程序调用 LINQ 中的用户定义函数?

我小时候在我的玩家类中为我的玩家制作了一个碰撞箱精灵。如何在游戏场景的 didBegin 方法中访问该碰撞箱精灵?