【问题标题】:adding UIImageview in titleview of UINavigationBar not working as expected在 UINavigationBar 的标题视图中添加 UIImageview 无法按预期工作
【发布时间】:2022-01-07 12:53:34
【问题描述】:

所以我尝试使用以下代码在 UINavigationbar 的 titleview 属性中添加 UIImageview。问题是,尽管我将特定大小放入包含 UIImageview 的 UIView ,其中包含图像,但它不能按预期工作。结果是导航栏中的图像比我使用 CGRectMake 设置的图像更大。同样在某些视图中,它与中心不对齐!我错过了什么?还有其他方法可以将图像添加到导航栏吗?

-(void)viewDidLayoutSubviews{
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,10.0f,16.0f)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,10.0f,16.0f)];
    
    CGSize imageSize = CGSizeMake(10, 16);
    CGFloat marginX = (self.navigationController.navigationBar.frame.size.width / 2) - (imageSize.width / 2);
    
    imageView.frame = CGRectMake(marginX, 11, imageSize.width, imageSize.height);
    imageView.contentMode = UIViewContentModeScaleAspectFit;

    UIImage *image = [UIImage sdk_imageName:@"logo.png"];

    [imageView setImage:image];
    [containerView addSubview:imageView];
    self.navigationItem.titleView=imageView;
}

【问题讨论】:

  • 你想要一个10x16 图像视图作为标题视图吗?
  • 您必须使用自动布局约束来调整标题视图的大小。设置框架没有任何作用。

标签: ios objective-c uinavigationbar


【解决方案1】:

更简单的方法...对图像视图大小使用自动布局约束...自动处理居中。

10x16 图像视图为titleView

UIImage *img = [UIImage imageNamed:@"logo"];
UIImageView *titleImageView = [[UIImageView alloc] initWithImage:img];
titleImageView.contentMode = UIViewContentModeScaleAspectFit;

titleImageView.translatesAutoresizingMaskIntoConstraints = NO;

[NSLayoutConstraint activateConstraints:@[

    [titleImageView.widthAnchor constraintEqualToConstant:10.0],
    [titleImageView.heightAnchor constraintEqualToConstant:16.0],
    
]];

self.navigationItem.titleView = titleImageView;

【讨论】:

  • 感谢您的回答,它有效,但我需要 titleview imageview 变得比导航栏的高度尺寸更大。这可能吗?
  • @stefanosn - 在您发布的代码中,您似乎正在尝试使用10x16 图像作为titleView。不清楚你的意思是让它“大于导航栏的高度大小”。也许模拟你正在尝试做的事情的图像?
  • 请检查此答案stackoverflow.com/questions/47121427/… 我有同样的问题,但无法解决。我尝试使用 cliptobounds 属性,但似乎不起作用......感谢任何帮助。
  • @stefanosn - 好的,这是一个非常不同的问题。基于多年来看到的类似问题,共识似乎是:制作自己的自定义视图,而不是使用内置的UINavigationBar
  • 我必须用导航栏来做。没有时间更改实施。
【解决方案2】:

我查看了您的代码,发现了一些框架问题,并且在底部您没有在 titleview 中传递容器视图,这不会按预期呈现视图。

请试试这个。

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];
UIImageView *imageView = [[UIImageView alloc] init];
//containerView.backgroundColor = [UIColor redColor];

CGSize imageSize = CGSizeMake(60, self.navigationController.navigationBar.frame.size.height);
CGFloat marginX = (self.navigationController.navigationBar.frame.size.width / 2) - (imageSize.width / 2);

imageView.frame = CGRectMake(marginX, 0, imageSize.width, imageSize.height);
imageView.contentMode = UIViewContentModeScaleAspectFit;

UIImage *image = [UIImage imageNamed: @"2.jpg"];

[imageView setImage:image];
[containerView addSubview:imageView];
self.navigationItem.titleView=containerView;

结果:

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    相关资源
    最近更新 更多