如何把图像放在UIViewController的navigationBar的中心?

我有一个在UIViewController的viewDidLoad中使用的代码片段。 我没有错误。 图像存在。 我得到的背景,但没有形象。 图像是一种标志。

if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) { /* Background of navigationBar. */ UIImage * navigationBarImage = [UIImage imageNamed:@"01_navbar_portrait.png"]; [self.navigationController.navigationBar setBackgroundImage:navigationBarImage forBarMetrics:UIBarMetricsDefault]; /* Image in navigationBar */ UIImage * logoInNavigationBar = [UIImage imageNamed:@"01_logo.png"]; UIImageView * logoView = [[UIImageView alloc] init]; [logoView setImage:logoInNavigationBar]; self.navigationController.navigationItem.titleView = logoView; } 

UINavigationController通过查看导航堆栈顶部视图控制器的navigationItem属性来pipe理导航栏。 因此,要将视图更改为徽标,您需要在使用徽标的视图控制器(即根视图控制器或另一个被压入栈中的视图控制器)中进行设置。

在视图控制器的viewDidLoad中做这样的事情:

 UIImage* logoImage = [UIImage imageNamed:@"logo.png"]; self.navigationItem.titleView = [[UIImageView alloc] initWithImage:logoImage]; 

在你的情况下,你正在设置错误的导航项目:

 // Oops... self.navigationController.navigationItem.titleView = logoView; // Should be this: self.navigationItem.titleView = logoView; 

首先,我们必须创build一个大小与导航栏相同的视图,然后添加一个图像视图,并设置其框架,因为它看起来在导航栏的中心。它适用于所有ios版本,并自动根据设备的帧大小视网膜或正常),像魔术一样工作。

 UIView *headerView = [[UIView alloc] init]; headerView.frame = CGRectMake(0, 0, 320, 44); UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Header.png"]]; imgView.frame = CGRectMake(75, 0, 150, 44); imgView.contentMode = UIViewContentModeScaleAspectFit; [headerView addSubview:imgView]; navigationCtrl.navigationBar.topItem.titleView = headerView; [headerView release]; [imgView release]; 

迅速:

 var logoImage:UIImage = UIImage(named: "logo_text")! self.navigationItem.titleView = UIImageView(image: logoImage) 

也许不是你的意思,但我跑到这个页面寻找一种方式为导航栏提供一个居中的背景图像 ,所以万一你在这里…这是一种方法。

从另外一个答案中偷一些,你可以将你的图像分解成前景和背景,然后build立一个新的图像,拉伸背景并居中前景,然后将其设置为导航栏的背景图像。 构build图像的工作原理如下:

 // build an image by stretching the bg, then merging it with the fg CGSize barSize = self.navController.navigationBar.frame.size; UIImage *fg = [UIImage imageNamed:@"some_fg"]; UIImage *bg = [[UIImage imageNamed:@"some_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0.f,1.f,0.f,1.f)]; UIGraphicsBeginImageContextWithOptions(barSize, NO, 0.0); [bg drawInRect:CGRectMake(0, 0, barSize.width, barSize.height)]; [fg drawInRect:CGRectMake((barSize.width-fg.size.width)/2.f, 0, fg.size.width, fg.size.height)]; // grab the merged images UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 
 extension UINavigationController { func addLogoImage(image: UIImage, navItem: UINavigationItem) { let imageView = UIImageView(image: image) imageView.contentMode = .scaleAspectFit imageView.translatesAutoresizingMaskIntoConstraints = false let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 44)) view.addSubview(imageView) navItem.titleView = view imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true imageView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true imageView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true view.heightAnchor.constraint(equalTo: navigationBar.heightAnchor).isActive = true view.centerXAnchor.constraint(equalTo: navigationBar.centerXAnchor).isActive = true view.centerYAnchor.constraint(equalTo: navigationBar.centerYAnchor).isActive = true } } 

这是我正在使用的。

当然,您可能需要更多的限制,以免与右侧和左侧的条形button项目发生冲突。

你只需指定它的框架

 logoView.frame = CGRectMake(initialize frame here); 

然后使用以下内容

 [self.navigationItem setTitleView:logoView];