调整navigationItem.titleView的框架? [英] Adjusting navigationItem.titleView's frame?

查看:99
本文介绍了调整navigationItem.titleView的框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到titleView的位置取决于右侧按钮标题。如何将titleView的框架设置在中心呢?

I noticed that the titleView's position depends on the rightbarbutton title. How can I set the frame of the titleView to be in the center instead?

我尝试过设置titleView.frame,但无济于事。

I've tried setting titleView.frame, but to no avail.

以下是代码:

UIButton *titleLabel = [UIButton buttonWithType:UIButtonTypeCustom];
[titleLabel setTitle:@"Right Eye" forState:UIControlStateNormal];
[titleLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[titleLabel setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
titleLabel.frame = CGRectMake(100, 0, 180, 44);
titleLabel.titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.titleLabel.font = [UIFont boldSystemFontOfSize:20.0];
titleLabel.titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.3];
//  titleLabel.titleLabel.textAlignment = UITextAlignmentCenter; (this doesn't work either)
[titleLabel addTarget:self action:@selector(titleTap:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titleLabel;
self.navigationItem.titleView.frame = CGRectMake(100, 0, 180, 44); //(this doesn't work either)

推荐答案

视图控制器的viewWillAppear之间的某个时间: &安培; viewDidAppear :,导航栏正在重新定位你的titleView并调整大小以适应它。它最终会被取消,因为导航栏更喜欢不调整titleView以适应按钮之间。它似乎试图将标题视图放在中心,如果它足够小,但如果没有将你的titleView偏离中心,然后我认为只有最后的手段才会调整大小。结果是titleView占据了两个按钮之间的所有空间,如果它的textAlignment居中,那么它将居中于那个空间,但不是相对于整个屏幕居中。您可以在屏幕截图中看到这一点。

Sometime in between your view controller's viewWillAppear: & viewDidAppear:, the navigation bar is repositioning your titleView and resizing it to fit if necessary. It can end up uncentered because the navigation bar is prefering not to resize your titleView to fit between the buttons. It seems to try to center the title view if it's small enough, but if not will move your titleView off-center, and then I think only as last resort will resize. The effect is that the titleView is taking up all the space between the two buttons, if its textAlignment is centered then it will centered in that space though not the centered with respect to the whole screen. You can see this in your screen shot.

一个答案是让你的titleView更窄,导航栏可以居中,所以当你设置时尝试大约160而不是180你的titleView.frame。很糟糕的是,当您以编程方式创建视图时,必须将该宽度的估计值放入代码中。什么是好的是在保持居中的同时最大化空间的方法。

One answer is to make your titleView narrower so the navigation bar can center it, so try about 160 instead of 180 when you set your titleView.frame. It sucks that, when creating the view programmatically like you are, one has to put an estimate for that width into the code. What would be good is a way to maximize that space while staying centered.

可以从titleView作为屏幕宽度开始,然后在导航栏改变titleView之后。框架,在视图控制器的viewDidAppear中自己再次更新它:使用其调整后的titleView.frame.origin.x和size.width以及屏幕宽度,您可以计算左边和右边的最大值。右边距,然后将origin.x设置为,size.width到屏幕宽度减去那个时间2.然而,直到推动的视图已经动画化并且之后的移位可见之后,它才会生效。您可以在viewWillAppear中隐藏titleView:然后在viewDidAppear中取消隐藏:在居中后,不要使用偏移的titleView滑入,然后移动,它会滑入而没有出现的标题。

It's possible to start with the titleView being the screen width, then after the navigation bar changes titleView.frame, update it again yourself in the view controller's viewDidAppear:. Using its adjusted titleView.frame.origin.x and size.width along with the screen width, you can calculate the largest of the left & right margins, then set the origin.x to that, the size.width to the screen width minus that times 2. However that doesn't take effect until after the pushed view has animated in and the shift afterwards is visible. You could hide the titleView in viewWillAppear: then unhide in viewDidAppear: after centering it, so instead of sliding in with an off-center titleView which is then shifted, it would slide in with no title which then appears.

更好的可能性是让你的标题查看你自己的UIView(或UILabel或其中任何一个)的子类,并覆盖setFrame来调整自身大小以保持其超级视图的中心。如果我自己最终这样做,我会在这里发布。或者也许其他人知道另一个地方在更新后更改titleView的框架,但在视图滑入之前没有视图子类。

A better possibility is to make your titleView your own subclass of UIView (or UILabel or whichever) and override setFrame to resize itself to stay centered in its superview. If I ever end up doing this myself I'll post it here. Or maybe someone else knows another place to change the titleView's frame after its been updated but before the view slides in without a view subclass.

这篇关于调整navigationItem.titleView的框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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