是否可以将某些图像设置为导航栏的标题?
我认为NYTimes应用程序使用导航栏并且标题看起来像图像文件(看起来UINavigationBar
的原因是因为他们使用右键搜索)。
答案 0 :(得分:127)
您可以对UIImageView
属性使用UINavigationItem.titleView
,例如:
self.navigationItem.titleView = myImageView;
答案 1 :(得分:16)
我发现高度约为35px的透明.png效果很好。
- (void)awakeFromNib {
//put logo image in the navigationBar
UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
self.navigationItem.titleView = img;
[img release];
}
答案 2 :(得分:12)
您可以从故事板(从Xcode 7开始)直接执行此操作:
答案 3 :(得分:8)
我为UINavigationBar创建了一个自定义类别,如下所示
<强> UINavigationBar的+ CustomImage.h 强>
#import <UIKit/UIKit.h>
@interface UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image;
- (void) clearBackgroundImage;
- (void) removeIfImage:(id)sender;
@end
<强> 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
我从我的UINavigationController
调用它[[navController navigationBar] performSelectorInBackground:@selector(setBackgroundImage:) withObject:image];
答案 4 :(得分:7)
此行适用于您,我总是使用此
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"imageNavBar.png"] forBarMetrics:UIBarMetricsDefault];
答案 5 :(得分:5)
我修改了UINavigationBar + CustomImage.m以使标题对用户仍然可见。只需使用insertSubview:atIndex:而不是addSubview:
<强> 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
答案 6 :(得分:5)
这也很有效
[self.navigationController.navigationBar.topItem setTitleView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"YourLogo"]]];
答案 7 :(得分:3)
答案 8 :(得分:2)
对于那些但在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;
}
}
}
}
希望它有所帮助!
答案 9 :(得分:0)
只需使用
[navController.navigationBar insertSubview:myImage atIndex:0] ;
其中myImage的类型为UIImageView 和navController的类型为UINavigationController
答案 10 :(得分:0)
我修改了UINavigationBar + CustomImage代码,以便在不泄漏内存的情况下正常工作。
- (void)setBackgroundImage:(UIImage *)image
{
if (! image) return;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[self addSubview:imageView];
[imageView release];
}
- (void) clearBackgroundImage
{
// This runs on a separate thread, so give it it's own pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *mySubviews = self.subviews;
// Move in reverse direction as not to upset the order of elements in the array
for (int i = [mySubviews count] - 1; i >= 0; i--)
{
if ([[mySubviews objectAtIndex:i] isMemberOfClass:[UIImageView class]])
{
[[mySubviews objectAtIndex:i] removeFromSuperview];
}
}
[pool release];
}
答案 11 :(得分:0)
以下是如何使用C#.NET在(Xamarin的)MonoTouch中执行此操作
创建一个位于NavigationController中的UIViewConvrtoller,然后随时调用它:
someNiceViewControllerYouMade.NavigationController.NavigationBar
.InsertSubview(new UIImageView
(MediaProvider.GetImage(ImageGeneral.navBar_667x44)),0);
注意:MediaProvider只是一个获取图像的类。
此示例允许视图填充整个导航栏,并允许显示项目标题的文本。
答案 12 :(得分:0)
如果您在导航中来回导航时按钮消失,则会为我修复此问题:
NSArray *mySubviews = navigationBar.subviews;
UIImageView *iv = nil;
// Move in reverse direction as not to upset the order of elements in the array
for (int i = [mySubviews count] - 1; i >= 0; i--)
{
if ([[mySubviews objectAtIndex:i] isMemberOfClass:[UIImageView class]])
{
NSLog(@"found background at index %d",i);
iv = [mySubviews objectAtIndex:i];
[[mySubviews objectAtIndex:i] removeFromSuperview];
[navigationBar insertSubview:iv atIndex:0];
}
}
答案 13 :(得分:0)
ios5.0引入了一堆功能来自定义标准元素的外观。如果您不想使用ImageView作为标题,另一种方法是使用背景图像和自定义字体/颜色自定义所有UINavbars的外观。
- (void) customiseMyNav
{
// Create resizable images
UIImage *portraitImage = [[UIImage imageNamed:@"nav_bar_bg_portrait"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *landscapeImage = [[UIImage imageNamed:@"nav_bar_bg_landscape"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Set the background image
[[UINavigationBar appearance] setBackgroundImage:portraitImage forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:landscapeImage forBarMetrics:UIBarMetricsLandscapePhone];
// set the title appearance
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:50.0/255.0 green:150.0/255.0 blue:100/255.0 alpha:1.0],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.6],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Arial-Bold" size:0.0],
UITextAttributeFont,
nil]];
}
答案 14 :(得分:0)
在MonoTouch中,您可以使用:
this.NavigationItem.TitleView = myImageView;
答案 15 :(得分:0)
使用 SWIFT 将图像添加到naviagtionBar,可以缩放以适合和剪切到边界。您可以在视图控制器viewDidLoad()函数中调用此函数。
func setupNavigationBarWithTitleImage(titleImage: UIImage) {
let imageView = UIImageView(image: titleImage)
imageView.contentMode = .ScaleAspectFit
imageView.clipsToBounds = true
navigationItem.titleView = imageView
}