无法在导航栏的中央设置titleView,因为后退按钮 [英] Can't set titleView in the center of navigation bar because back button

查看:83
本文介绍了无法在导航栏的中央设置titleView,因为后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用图像视图在导航栏中显示图像.问题是由于使用后退按钮,我无法将其正确设置在中央.我检查了相关问题,并且较早解决了几乎相同的问题,但是这次我不知道.

I'm using an image view to display an image in my nav bar. The problem is that I can't set it to the center correctly because of the back button. I checked the related questions and had almost the same problem earlier that I solved, but this time I have no idea.

之前,我使用伪造的条形按钮解决了这个问题,因此我尝试在右侧(和左侧)添加伪造的条形按钮,但这没有帮助.

Earlier I solved this problem with fake bar buttons, so I tried to add a fake bar button to the right (and left) side, but it doesn't helped.

- (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在同一侧具有条形按钮.有什么解释为什么它可以通过编程方式创建的条形按钮使用,但不能与常用的后退按钮一起使用?

I think it should be workin fine because in this case the titleView has bar buttons on the same side. Is there any explanation why it worked with bar buttons that was created programmatically but doesn't works with the common back button?

推荐答案

UINavigationBar就会自动将其titleView居中.如果标题未居中,则表示标题视图太宽而无法居中;如果UIImageView设置了backgroundColor,那么您将看到正在发生的情况.

UINavigationBar automatically centers its titleView as long as there is enough room. If the title isn't centered that means that the title view is too wide to be centered, and if you set the backgroundColor if your UIImageView you'll see that's exactly what is happening.

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

The title view is too wide because that navigation bar will automatically resize the title to hold its content, using -sizeThatFits:. This means that your title view will always be resized to the size of your image.

两个可能的修复程序:

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

  1. The image you're using is way too big. Use a properly sized 44x44 pt image with 2x and 3x versions.

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

Wrap UIImageView inside of a regular UIView to avoid resizing.

示例:

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;

这篇关于无法在导航栏的中央设置titleView,因为后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆