gpt4 book ai didi

ios - 无法在导航栏中心设置titleView,因为返回按钮

转载 作者:IT老高 更新时间:2023-10-28 11:36:48 27 4
gpt4 key购买 nike

我正在使用 ImageView 在导航栏中显示图像。问题是由于后退按钮,我无法将其正确设置为中心。我查了相关问题,之前解决的问题几乎相同,但这次我不知道。

之前我用假条形按钮解决了这个问题,所以我尝试在右侧(和左侧)添加一个假条形按钮,但没有帮助。

- (void) searchButtonNavBar {


CGRect imageSizeDummy = CGRectMake(0, 0, 25,25);

UIButton *dummy = [[UIButton alloc] initWithFrame:imageSizeDummy];

UIBarButtonItem
*searchBarButtonDummy =[[UIBarButtonItem alloc] initWithCustomView:dummy];
self.navigationItem.rightBarButtonItem = searchBarButtonDummy;

}


- (void)setNavBarLogo {

[self setNeedsStatusBarAppearanceUpdate];
CGRect myImageS = CGRectMake(0, 0, 44, 44);
UIImageView *logo = [[UIImageView alloc] initWithFrame:myImageS];
[logo setImage:[UIImage imageNamed:@"color.png"]];
logo.contentMode = UIViewContentModeScaleAspectFit;
self.navigationItem.titleView = logo;
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0f, 0.0f) forBarMetrics:UIBarMetricsDefault];

}

我认为它应该可以正常工作,因为在这种情况下 titleView 在同一侧有条形按钮。有什么解释为什么它可以与以编程方式创建的条形按钮一起使用,但不能与常见的后退按钮一起使用?

最佳答案

只要有足够的空间,

UINavigationBar 就会自动将其 titleView 居中。如果标题未居中,则意味着标题 View 太宽而无法居中,如果您在 UIImageView 中设置了 backgroundColor,您将看到正在发生的事情。

标题 View 太宽,因为导航栏会使用 -sizeThatFits: 自动调整标题大小以容纳其内容。这意味着您的标题 View 将始终调整为您的图像的大小。

两个可能的修复:

  1. 您使用的图片太大了。使用具有 2x 和 3x 版本的适当大小的 44x44 pt 图像。

  2. 将 UIImageView 包裹在常规 UIView 中以避免调整大小。

例子:

UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.jpeg"]];
imageView.contentMode = UIViewContentModeScaleAspectFit;

UIView* titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
imageView.frame = titleView.bounds;
[titleView addSubview:imageView];

self.navigationItem.titleView = titleView;

关于ios - 无法在导航栏中心设置titleView,因为返回按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28121388/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com