UINavigationItem 居中标题 [英] UINavigationItem centering the title

查看:29
本文介绍了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 属性更改等).

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天全站免登陆