UINavigationBar中心自定义标题? [英] center custom title in UINavigationBar?

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

问题描述

我有一个自定义UINavigationBar标题和一个自定义后退按钮。
我的问题是标题不是以iPhone为中心。
就好像我的后退按钮将标题推到了右边。任何想法我怎么能集中它?

I have a custom UINavigationBar title and a custom back button. My problem is that the title is not centered on the iPhone. It is as if my back button is pushing the title over to the right. Any Idea how I can center it?

int height = self.navigationController.navigationBar.frame.size.height;
int width = self.navigationController.navigationBar.frame.size.width;


UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width + 300, 20)];
navLabel.backgroundColor = [UIColor whiteColor];
navLabel.textColor = [UIColor redColor];
navLabel.font = [UIFont fontWithName:@"Helvetica" size:30];
navLabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = navLabel;
[navLabel release];
((UILabel *)self.navigationItem.titleView).text = self.title;

谢谢!

编辑我删除了多余的代码并添加了这张图片:

edit I removed superfluous code and added this picture:

注意标题是如何被推过来容纳按钮....

Notice how the title is pushed over to accomodate the button....

推荐答案

iOS正在执行此操作,因为您初始化的帧始终是300 +宽度像素。它试图使整个框架居中,框架比它想要的空间大(因为按钮),因此你的标签被推到右边。
你需要做的是给navLabel的框架提供它所需的最小尺寸。

iOS is doing this because the frame you initialize is alway 300+width pixels. It is trying to center the full frame, the frame is larger than the space it wants to fit it in (because of the button) and therefore your label gets pushed to the right. What you need to do is give the Frame of the navLabel the minimum size it needs.

所以如果你的文字只有100px宽,但框架是400px,然后iOS试图将400px集中在Navigation标头内,并且没有足够的空间。当您将大小设置为所需的实际100px时,iOS会正确地对齐您的标题,因为有足够的空间来居中100px。

So if your text is only 100px wide, but the frame is 400px, then iOS is trying to center the 400px inside the Navigation header, and doesn't have enough space. When you set the size to the actual 100px that is needed, iOS will center your header correctly, because there is plenty of space to center 100px.

下面的代码片段应该帮助您检测框架所需的最小尺寸,具体取决于您尝试放入的字体和文本。
确保标签的框架尽可能小,但不超过最大宽度。
(导航栏的宽度)。

The code snippet below should help you to detect the minimum size your frame needs, depending on the font and the text you try to put in. Make sure the frame of the label is as small as possible, but does not exceed the max width. (the width of the navigation bar).

UIFont* titleFont = [UIFont fontWithName:@"Helvetica" size:30];
CGSize requestedTitleSize = [titleText sizeWithAttributes:@{NSFontAttributeName: titleFont}]; 
CGFloat titleWidth = MIN(maxTitleWidth, requestedTitleSize.width);

UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleWidth, 20)];
navLabel.backgroundColor = [UIColor whiteColor];
navLabel.textColor = [UIColor redColor];
navLabel.font = [UIFont fontWithName:@"Helvetica" size:30];
navLabel.textAlignment = NSTextAlignmentCenter;
navLabel.text = titleText;
self.navigationItem.titleView = navLabel;

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

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