是否可以将某些图像设置为导航栏的标题?我可以将图像设置为UINavigationBar的标题吗?
我觉得NYTimes应用程序使用了一个导航栏,标题看起来像图像文件(它似乎是UINavigationBar
的原因是因为它们使用右键搜索)。
是否可以将某些图像设置为导航栏的标题?我可以将图像设置为UINavigationBar的标题吗?
我觉得NYTimes应用程序使用了一个导航栏,标题看起来像图像文件(它似乎是UINavigationBar
的原因是因为它们使用右键搜索)。
您可以使用UIImageView
为UINavigationItem.titleView
财产,是这样的:
self.navigationItem.titleView = myImageView;
我创建了一个自定义的类别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];
我修改了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
我发现,在高度约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];
}
只需使用
[navController.navigationBar insertSubview:myImage atIndex:0] ;
其中MYIMAGE是类型的UIImageView 和navController是类型的UINavigationController
的我修改了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];
}
以下是你将如何使用C#.NET这样做的(Xamarin的)的MonoTouch
创建UIViewConvrtoller是在NavigationController然后随时调用此:
someNiceViewControllerYouMade.NavigationController.NavigationBar
.InsertSubview(new UIImageView
(MediaProvider.GetImage(ImageGeneral.navBar_667x44)),0);
注意:MediaProvider只是一个提取图像的类。
此示例允许视图填充完整的导航栏,并让项目标题的文本也显示出来。
如果当你来回导航导航您的按钮消失,这个固定对我来说:
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];
}
}
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]];
}
在MonoTouch中您可以使用此:
this.NavigationItem.TitleView = myImageView;
此行会为你工作,我总是用这
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"imageNavBar.png"] forBarMetrics:UIBarMetricsDefault];
,这也非常得
[self.navigationController.navigationBar.topItem setTitleView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"YourLogo"]]];
对于那些有同样的错误,但在Xamarin Forms
,解决方案离子是建立在iOS
应用一个Renderer
,并设置图像,像这样:
[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;
}
}
}
}
希望它可以帮助别人!
将图像添加到naviagtionBar中SWIFT可缩放以适合并剪裁到边界。你可以在视图控制器的viewDidLoad()函数中调用这个函数。
做它迅速用故事板和@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
}
}
}
然后在属性检查器中只选择一个图像:
,等待结果的第二个:
因此,设置视图应该在故事板中。
不一定与问题相关,但是如果您想要在默认标题和图片标题之间切换,您可以通过将`self.navigationItem.titleView`设置为`nil`回到默认的标题 – 2011-12-02 10:46:30
这不再适用于iOS6。图像会变形并变形。 – 2012-12-07 16:52:46
在IOS6中,您现在可以为导航栏设置背景图像。但是,如果您正在使用此代码并看到图像拉伸,那么您无法在放置在titleView中的UIImageView上正确设置contentMode。 – 2012-12-07 20:18:59