繁体   English   中英

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

[英]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 中的导航栏中心添加图像?

提前致谢。

设置导航标题视图如下

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;

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

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

输出:

在此处输入图片说明

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

在 WWDC Session 204, Updating your app for iOS 11, 指出 UIToolBar 和 UINavigationBar 已升级为使用自动布局,为了避免零大小的自定义视图,你应该了解一些关于它的事情。

UINavigation 和 UIToolBar 提供位置,不关心它。

您必须为自定义视图的大小提供以下其中一项:

  • 宽度和高度的约束。
  • 实现内在内容大小
  • 通过约束连接你的子视图。

回答你的问题,我用 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;
}

或者设置aspect mode为aspectFit

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

如果您想观看 Session, Session 204

这里要提到的一件事是为 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;
 viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];

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

祝你好运

尝试这个 :

 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 *navbarImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
navbarImageView.contentMode = UIViewContentModeScaleAspectFit;
self.navigationBar.titleView = navbarImageView;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM