我在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;
}
答案 0 :(得分:53)
UINavigationController
通过查看导航堆栈上最顶层视图控制器的navigationItem
属性来管理导航栏。因此,要将视图更改为徽标,您需要在使用徽标的视图控制器(即根视图控制器或其他被推入堆栈的控制器)中进行设置。
在视图控制器的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;
答案 1 :(得分:9)
首先,我们必须创建一个与导航栏大小相同的视图 然后添加一个图像视图并设置其框架,因为它看起来居中 在导航栏中。它适用于所有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];
答案 2 :(得分:5)
var logoImage:UIImage = UIImage(named: "logo_text")!
self.navigationItem.titleView = UIImageView(image: logoImage)
答案 3 :(得分:3)
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
}
}
这是我正在使用的内容。
当然,您可能需要更多约束,以免与右侧和左侧条形按钮项冲突。
答案 4 :(得分:2)
也许不是你的意思,但是我跑到这个页面寻找一种方法来为导航栏提供一个居中的背景图像,所以如果你在这里的话......这里是一种方式。
从another answer偷一点,您可以将图像分解为前景和背景,然后构建一个新的图像,拉伸背景并使前景居中,然后将其设置为导航栏的背景图像。构建图像的方式如下:
// 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();
答案 5 :(得分:1)
您只需通过
指定它的框架logoView.frame = CGRectMake(initialize frame here);
然后使用以下
[self.navigationItem setTitleView:logoView];
答案 6 :(得分:0)
func centeredNavBarImage (){
let navcontroller = navigationController!
let image = #imageLiteral(resourceName: "yourImage")
let imageView = UIImageView(image:image)
let bannerWidth = navcontroller.navigationItem.accessibilityFrame.size.width
let bannerHeight = navcontroller.navigationBar.frame.size.height
let bannerX = bannerWidth / 2 - image.size.width / 2
let bannerY = bannerHeight / 2 - image.size.height / 2
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
}
这是https://youtu.be/bLkuu_fmlsU
的修改代码bannerWidth考虑了我在导航栏右侧具有的刷新按钮项。似乎可以工作。
在ViewDidLoad上运行功能