Iphone/Ipad 将图像和文本添加到导航标题
我只是想将图像和文本添加到 navigationItem 标题。这就是我正在做的,但它只显示图像而不显示标题。这看起来似乎很容易。
例子 (
self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo_small_white.png"]] autorelease];
self.navigationItem.title = @"Company";
I am simply trying to add an image and text to the navigationItem title. This is what I am doing, but it only shows the image and not the title. This seems like it would be pretty easy.
Example (
self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo_small_white.png"]] autorelease];
self.navigationItem.title = @"Company";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我的代码:
Here is my code:
通过设置自定义标题视图,您将替换通常显示标题文本的标准视图。来自
UINavigationItem
中titleView
的文档:因此,您需要添加自己的视图(可能是
UILabel
)来显示标题 - 一种方法是添加UILabel
和UIImageView< /code> 作为容器
UIView
的子视图,并将 that 设置为titleView
。By setting a custom title view, you're replacing the standard view that normally shows the title text. From the documentation for
titleView
inUINavigationItem
:So you need to add your own view (possibly a
UILabel
) to display your title — one way of doing this would be to add aUILabel
and theUIImageView
above as subviews of a containerUIView
, and set that as thetitleView
.在 Swift 2 中:
相关问题:titleView 自动居中,如果上面的示例不是这种情况,那么您可能使用较小的屏幕尺寸(例如 iPhone)。只需根据需要将 UIView 和 UILabel 的宽度从 300 更改为更小即可。当有空间时它会弹回到中心。
In Swift 2:
Related gotcha: titleView centres automatically, if this isn't the case with the above example then you're likely using a smaller screensize (eg. iPhone). Simply change the width of the UIView and UILabel from 300 to less, as suited. It'll snap back to centre when there is room.
这可能是做到这一点的最短方法。
This maybe the shortest way to do that.