无法在导航栏的中心设置titleView,因为后退按钮

rih*_*ihe 48 objective-c uinavigationbar ios

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

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

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

}
Run Code Online (Sandbox Code Playgroud)

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

Dar*_*ren 93

UINavigationBartitleView只要有足够的空间,它就会自动居中.如果标题不居中意味着标题视图太宽而无法居中,如果您设置了backgroundColor,如果您UIImageView将看到正是发生的事情.

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

两个可能的修复:

  1. 你正在使用的图像太大了.使用尺寸合适的44x44 pt图像,2x和3x版本.

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

例:

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;
Run Code Online (Sandbox Code Playgroud)


ped*_*uan 16

Swren 3版Darren第二种方式的一个例子:

let imageView = UIImageView(image: UIImage(named: "test"))
    imageView.contentMode = UIViewContentMode.scaleAspectFit
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
    imageView.frame = titleView.bounds
    titleView.addSubview(imageView)

self.navigationItem.titleView = titleView
Run Code Online (Sandbox Code Playgroud)


小智 7

我建议你覆盖函数 - (void)setFrame:(CGRect)fram像这样:

- (void)setFrame:(CGRect)frame  { 

    [super setFrame:frame]; //systom function

    self.center = CGPointMake(self.superview.center.x, self.center.y);   //rewrite function 

}
Run Code Online (Sandbox Code Playgroud)

这样titleView.center始终是正确的位置


Pow*_*wHu 5

不要使用titleView.

只需将您的图像添加到navigationController.navigationBar即可

CGRect myImageS = CGRectMake(0, 0, 44, 44);
UIImageView *logo = [[UIImageView alloc] initWithFrame:myImageS];
[logo setImage:[UIImage imageNamed:@"color.png"]];
logo.contentMode = UIViewContentModeScaleAspectFit;
logo.center = CGPointMake(self.navigationController.navigationBar.width / 2.0, self.navigationController.navigationBar.height / 2.0);
logo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self.navigationController.navigationBar addSubview:logo];
Run Code Online (Sandbox Code Playgroud)

  • 然后,如果您返回同一个导航控制器,则视图位于每个控制器上. (4认同)

小智 5

群力对我很有帮助.这是swift 2.3代码:

override var frame: CGRect {
    set(newValue) {
        super.frame = newValue

        if let superview = self.superview {
            self.center = CGPoint(x: superview.center.x, y: self.center.y)
        }
    }

    get {
        return super.frame
    }
}
Run Code Online (Sandbox Code Playgroud)