如何在Objective-C的导航栏中心添加图像?

问题描述 投票:13回答:8

我正在开发IOS,我使用以下代码设置navigationBar的背景。

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

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

如何在Objective-C的导航栏中心添加图像?

提前致谢。

ios image navigationbar
8个回答
33
投票

设置导航标题视图如下所示

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


EDIT:
if you have big image then use below code
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;

6
投票

正如你所说的那样为应用程序设置一个图标。您可能希望为此展示整个应用程序的图标。

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];

输出:


2
投票

这里要提到的一件事是为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;

1
投票
 viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];

1
投票

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

在WWDC Session 204中,为iOS 11更新应用程序时声明UIToolBar和UINavigationBar已升级为使用自动布局,为了避免零大小的自定义视图,您应该了解一些相关信息。

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


0
投票

试试这个 :

 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];

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

0
投票

如果有人需要在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()
}

祝好运

© www.soinside.com 2019 - 2024. All rights reserved.