UINavigationItem以标题为中心 [英] UINavigationItem centering the title

查看:87
本文介绍了UINavigationItem以标题为中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个左侧和右侧都有按钮的导航栏。我有一个 customTitlelabel ,我将其设置为 UINavigationItem 的titleView。

I have a navigationBar with both Left and Right bar buttons on each side. I have a customTitlelabel which I set as the titleView of the UINavigationItem.


[self.navigationItem setTitleView:customTitleLabel];

[self.navigationItem setTitleView:customTitleLabel];

现在一切都很好。问题是,rightbarButton的大小是动态的,基于我在其中一个文本字段中得到的输入。

All is fine now. The problem, the size of the rightbarButton is dynamic based on the input I get in one of the text fields.

因此,标题会根据按钮之间的可用空间自动居中。

Therefore the title is automatically centered based on the available space between the buttons.

如何将标题设置为固定位置?

how can i set the title to a fixed position?

推荐答案

你不能直接做你想做的事 - 你的标题视图的位置是你无法控制的(当你管理时)通过 UINavigationBar )。

You can't do what you want directly -- the position of your title view is out of your control (when managed by UINavigationBar).

但是,至少有两种策略可以达到你想要的效果:

However, there are at least two strategies to get the effect you want:

1)添加标题查看不是导航栏的正确标题视图,而是作为 UINavigationBar 的子视图。 (注意:这不是'正式'批准,但我已经看到它完成了,并且工作。显然你必须注意你的标题标签覆盖按钮的位,并处理不同尺寸的导航条,以适应不同的方向等。 - 有点繁琐。)

1) Add the title view not as the 'proper' title view of the nav bar, but as a subview of the UINavigationBar. (Note: this is not 'officially' sanctioned, but I've seen it done, and work. Obviously you have to watch out for your title label overwriting bits of the buttons, and handle different size nav bars for different orientations, etc. -- a bit fiddly.)

2)制作一个智能的UIView子类,在计算出有效显示子视图的位置显示给定的子视图(可能是你的UILabel)完美地集中在屏幕上。为此,您的智能UIView子类将通过更改位置( frame frame 属性更改等) code>)标签子视图。

2) Make an intelligent UIView subclass that displays a given subview (which would be your UILabel) at a position calculated to effectively show the subview perfectly centered on the screen. In order to do this, your intelligent UIView subclass would respond to layout events (or frame property changes etc.) by changing the position (frame) of the label subview.

就个人而言,我最喜欢方法2)的想法。

Personally, I like the idea of approach 2) the best.

这篇关于UINavigationItem以标题为中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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