Ios 我可以将图像设置为UINavigationBar的标题吗?
是否可以将某些图像设置为导航栏的标题Ios 我可以将图像设置为UINavigationBar的标题吗?,ios,cocoa-touch,Ios,Cocoa Touch,是否可以将某些图像设置为导航栏的标题 我认为《纽约时报》应用程序使用了一个导航栏,标题看起来像图像文件(之所以看起来像UINavigationBar,是因为他们使用右键进行搜索)。您可以使用UIImageView来查看UINavigationItem.titleView属性,例如: self.navigationItem.titleView = myImageView; 我为UINavigationBar创建了一个自定义类别,如下所示 UINavigationBar+CustomImage.h
我认为《纽约时报》应用程序使用了一个导航栏,标题看起来像图像文件(之所以看起来像
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,使标题对用户仍然可见。只需使用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(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
#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
#导入“UINavigationBar+CustomImage.h”
@实现UINavigationBar(CustomImage)
-(无效)setBackgroundImage:(UIImage*)图像{
if(image==NULL)返回;
UIImageView*imageView=[[UIImageView alloc]initWithImage:image];
imageView.frame=CGRectMake(0,0320,44);
[自插入子视图:imageView索引:0];
[图像视图发布];
}
-(无效)clearBackgroundImage{
NSArray*子视图=[自子视图];
对于(inti=0;i,我发现高度约为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代码,以在不泄漏内存的情况下正常工作
[[navController navigationBar] performSelectorInBackground:@selector(setBackgroundImage:) withObject:image];
- (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中实现这一点
创建位于NavigationController中的UIViewConverToller,然后随时调用它:
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作为标题,另一种方法是使用背景图像和自定义字体/颜色来定制所有UINAVbar的外观
- (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"]]];
您可以直接从故事板(从Xcode 7开始)执行此操作:
在视图控制器的主视图之外创建视图。它可以是嵌套视图,也可以只是图像
将导航项添加到视图控制器
按住Ctrl键并从导航项中拖动,然后放到外部视图中
4.选择标题视图
对于那些在Xamarin表单中出现相同错误的人,解决方案是在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;
}
}
}
}
希望它能帮助某些人!使用可缩放到合适大小并剪裁到边界的SWIFT将图像添加到导航栏。您可以在view Controller viewDidLoad()函数中调用此函数
func setupNavigationBarWithTitleImage(titleImage: UIImage) {
let imageView = UIImageView(image: titleImage)
imageView.contentMode = .ScaleAspectFit
imageView.clipsToBounds = true
navigationItem.titleView = imageView
}
使用故事板和@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
}
}
}
然后在“属性检查器”中,仅选择一个图像:
然后等待一秒钟,等待结果:
因此,在故事板中,设置视图就是它应该在的位置。FYI:我认为这条线需要释放。标题的UIImage大小应该是多少?我尝试了180*40,但它变得模糊了。您好。如果用户导航到其他视图(使用navigationController pushViewController),则标题仅在即时视图中可见,仅显示背景。如何解决此问题?不一定与问题相关,但如果您希望在默认标题和图像标题之间切换,可以通过将self.navigationItem.titleView
设置为nil
返回默认标题。此选项在iOS6中不再有效。图像将被拉伸并出现格式错误。在IOS6中,您现在可以为导航栏设置背景图像。但是,如果您使用此代码并看到图像被拉伸,则在标题视图中放置的UIImageView上未正确设置contentMode。@AuthmanApatira您是对的,是否可以发布IOS6的解决方案。对于IOS6添加[imageView setContentMode:UIViewContentModeScaleAspectFit];这是唯一一款适用于我的UINavigationViewController-谢谢!