Objective-C 如何在navigationBar 中心添加图片?

How to add a image at the center of navigationBar in Objective-C?

我在IOS开发,我用下面的代码给navigationBar.

设置背景
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bar-back"] forBarMetrics:UIBarMetricsDefault];

我想在 navigationBarnavigationBar 的中心添加图像,如下图所示。

如何在Objective-C中的navigationBar中心添加图片?

提前致谢。

像下面这样设置导航标题视图

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



编辑:
如果你有大图片然后使用下面的代码

UIImage *img = [UIImage imageNamed:@"1.png"];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[imgView setImage:img];
// setContent mode aspect fit
[imgView setContentMode:UIViewContentModeScaleAspectFit];
self.navigationItem.titleView = imgView;
 viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];

试试这个:

 UIImage *logo = [UIImage imageNamed:@"logo.png"];
 UIImageView *imageView = [[UIImageView alloc] initWithImage: logo];
 imageView.frame = CGRectMake(160, 2, 40, 40);
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:logo];

正如您所说,为应用程序设置一个图标。你可能喜欢这个,它会显示整个应用程序的图标。

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon"]];
CGSize imageSize = CGSizeMake(40, 40);
CGFloat marginX = (self.navigationController.navigationBar.frame.size.width / 2) - (imageSize.width / 2);

imageView.frame = CGRectMake(marginX, 0, imageSize.width, imageSize.height);
[self.navigationController.navigationBar addSubview:imageView];

输出:

这里要提到的一件事是为 UIImageView 使用宽度 3。这很重要——如果它是 2(或任何偶数),那么图像将变得模糊。设置 clipToBounds=NO 和 contentMode=UIViewContentModeScaleAspectFill 意味着图像将显示在图像视图的宽度之外(很好),并且会正确地按比例显示。

UIImageView *imageView = [[UIImageView alloc]
        initWithFrame:CGRectMake(0,0,3,44)];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = NO;
    imageView.image = [UIImage imageNamed:@"navigation-logo"];
    controller.navigationItem.titleView = imageView;

我遇到了同样的问题,因为 iOS 11 和 Xcode 9 开始使用自动布局而不是框架来管理导航栏,你应该注意你的导航栏布局。

在 WWDC Session 204 中,Updating your app for iOS 11,指出 UIToolBar 和 UINavigationBar 已升级为使用自动布局,并且为了避免 Zero-Sized 自定义Views 你应该知道的一些事情。

UINavigation 和 UIToolBar 提供位置,不用管它。

您必须使用以下之一提供自定义视图的大小:

  • 宽度和高度的限制。
  • 实施 intrinsicContentSize
  • 通过约束连接您的子视图。

回答你的问题,我用 ImageView 设置了 titleView,以两种不同的方式提供尺寸:

明确给出高度和宽度。

-(void)updateNavTitleView{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[ UIImage imageNamed:@"TitleViewImage"]];
    [imageView.widthAnchor constraintEqualToConstant:160].active = YES;
    [imageView.heightAnchor constraintEqualToConstant:44].active = YES;
    self.navigationItem.titleView = imageView;
}

或者设置纵横比模式为aspectFit

-(void) updateNavTitleView{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[ UIImage imageNamed:@"TitleViewImage"]];
    [imageView setContentMode:UIViewContentModeScaleAspectFit];
    self.navigationItem.titleView = imageView;
}

如果您想观看 Session、Session 204

UIImageView *navbarImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
navbarImageView.contentMode = UIViewContentModeScaleAspectFit;
self.navigationBar.titleView = navbarImageView;

如果有人需要在 Swift (4.2) 中执行上述操作:

写下下面的函数:

func addMiddleImage(){
    //Initialize your UIImage
    let yourImage = UIImage(named: "imageName")!.withRenderingMode(.alwaysOriginal)

    //Initialize a UIImageView
    let imageView = UIImageView(image: yourImage)

    //Set your Navigation Bar to the inialized UIImageView
    self.navigationItem.titleView = imageView

}

将其添加到您的 ViewDidLoad() 回调中:

 override func viewDidLoad() {
    super.viewDidLoad()
    self.addMiddleImage()
}

祝你好运