ios 我可以将图像设置为 UINavigationBar 的标题吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/289441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I set image as a title to UINavigationBar?
提问by Amit Vaghela
Is it possible to set some image as title of Navigation bar?
是否可以将一些图像设置为导航栏的标题?
I think NYTimes application used a Navigation bar and title is look like image file (the reason why it's seems UINavigationBar
is because they use right button to search).
我认为 NYTimes 应用程序使用了导航栏,标题看起来像图像文件(之所以看起来UINavigationBar
是因为他们使用右键进行搜索)。
回答by Kendall Helmstetter Gelner
You can use an UIImageView
for the UINavigationItem.titleView
property, something like:
您可以UIImageView
对UINavigationItem.titleView
属性使用 ,例如:
self.navigationItem.titleView = myImageView;
回答by Clifton Burt
I find that a transparent .png at about 35px in height has worked well.
我发现高度约 35 像素的透明 .png 效果很好。
- (void)awakeFromNib {
//put logo image in the navigationBar
UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
self.navigationItem.titleView = img;
[img release];
}
回答by Cinoss
You can do it right from storyboard (as of Xcode 7):
您可以直接从故事板(从 Xcode 7 开始):
- Create a view outside main view of view controller. It can be a nested view or just an image
- Add navigation item to your view controller
- Ctrl+ drag from navigation item and drop on outside view
- 在视图控制器的主视图之外创建一个视图。它可以是嵌套视图或只是图像
- 将导航项添加到您的视图控制器
- Ctrl+ 从导航项拖动并放在外部视图上
4.select title view
4.选择标题视图
回答by adam
I have created a custom category for UINavigationBar as follows
我为 UINavigationBar 创建了一个自定义类别,如下所示
UINavigationBar+CustomImage.h
UINavigationBar+CustomImage.h
#import <UIKit/UIKit.h>
@interface UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image;
- (void) clearBackgroundImage;
- (void) removeIfImage:(id)sender;
@end
UINavigationBar+CustomImage.m
UINavigationBar+CustomImage.m
#import "UINavigationBar+CustomImage.h"
@implementation UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image {
if (image == NULL) return;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(110,5,100,30);
[self addSubview:imageView];
[imageView release];
}
- (void) clearBackgroundImage {
NSArray *subviews = [self subviews];
for (int i=0; i<[subviews count]; i++) {
if ([[subviews objectAtIndex:i] isMemberOfClass:[UIImageView class]]) {
[[subviews objectAtIndex:i] removeFromSuperview];
}
}
}
@end
I invoke it from my UINavigationController
我从我的 UINavigationController 调用它
[[navController navigationBar] performSelectorInBackground:@selector(setBackgroundImage:) withObject:image];
回答by adam
This line will work for you, I always use this
这条线对你有用,我总是用这个
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"imageNavBar.png"] forBarMetrics:UIBarMetricsDefault];
回答by MrChrisBarker
This also works well too
这也很好用
[self.navigationController.navigationBar.topItem setTitleView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"YourLogo"]]];
回答by ck.
I modified the UINavigationBar+CustomImage.m to have the title still visible to the user. Just use insertSubview: atIndex: instead of addSubview:
我修改了 UINavigationBar+CustomImage.m 以使标题仍然对用户可见。只需使用 insertSubview: atIndex: 而不是 addSubview:
UINavigationBar+CustomImage.m
UINavigationBar+CustomImage.m
#import "UINavigationBar+CustomImage.h"
@implementation UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image {
if (image == NULL) return;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 320, 44);
[self insertSubview:imageView atIndex:0];
[imageView release];
}
- (void) clearBackgroundImage {
NSArray *subviews = [self subviews];
for (int i=0; i<[subviews count]; i++) {
if ([[subviews objectAtIndex:i] isMemberOfClass:[UIImageView class]]) {
[[subviews objectAtIndex:i] removeFromSuperview];
}
}
}
@end
回答by Bart?omiej Semańczyk
Do it quickly using storyboard and @IBDesignable
:
使用故事板快速完成并@IBDesignable
:
@IBDesignable class AttributedNavigationBar: UINavigationBar {
@IBInspectable var imageTitle: UIImage? = nil {
didSet {
guard let imageTitle = imageTitle else {
topItem?.titleView = nil
return
}
let imageView = UIImageView(image: imageTitle)
imageView.frame = CGRect(x: 0, y: 0, width: 40, height: 30)
imageView.contentMode = .scaleAspectFit
topItem?.titleView = imageView
}
}
}
Then in attributes inspector just select an image:
然后在属性检查器中选择一个图像:
and wait a second for result:
等待结果:
So setting view is there where it should be... in storyboard.
所以设置视图应该在那里......在故事板中。
回答by Dr TJ
For those who have the same error but in Xamarin Forms
, the solution is to create a Renderer
in iOS
app and set the image like so :
对于那些有相同错误但在 中的人Xamarin Forms
,解决方案是Renderer
在iOS
应用程序中创建一个并像这样设置图像:
[assembly: Xamarin.Forms.ExportRenderer(typeof(Xamarin.Forms.Page), typeof(MyApp.Renderers.NavigationPageRenderer))]
namespace MyApp.Renderers
{
#region using
using UIKit;
using Xamarin.Forms.Platform.iOS;
#endregion
public class NavigationPageRenderer : PageRenderer
{
public override void ViewDidLoad()
{
base.ViewDidLoad();
SetTitleImage();
}
private void SetTitleImage()
{
UIImage logoImage = UIImage.FromFile(ResourceFiles.ImageResources.LogoImageName);
UIImageView logoImageView = new UIImageView(logoImage);
if (this.NavigationController != null)
{
this.NavigationController.NavigationBar.TopItem.TitleView = logoImageView;
}
}
}
}
Hope it helps someone!
希望它可以帮助某人!
回答by user3378170
Add image to naviagtionBar with SWIFTthat scales to fit and clips to bounds. You can call this function inside the view controllers viewDidLoad() function.
使用SWIFT将图像添加到 naviagtionBar,该SWIFT可缩放以适合并剪辑到边界。您可以在视图控制器 viewDidLoad() 函数中调用此函数。
func setupNavigationBarWithTitleImage(titleImage: UIImage) {
let imageView = UIImageView(image: titleImage)
imageView.contentMode = .ScaleAspectFit
imageView.clipsToBounds = true
navigationItem.titleView = imageView
}