UINavigationBar中心自定义标题?

时间:2012-03-29 07:39:28

标签: iphone uinavigationbar title

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

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;

谢谢!

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

Picture of title off ceter

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

7 个答案:

答案 0 :(得分:40)

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

因此,如果您的文本只有100px宽,但帧是400px,那么iOS正试图将400px置于导航标题内,并且没有足够的空间。当您将大小设置为所需的实际100px时,iOS将正确地对齐您的标题,因为有足够的空间来居中100px。

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

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;

答案 1 :(得分:5)

之前我遇到过同样的问题。我有一个带有右键和左键的UINavigationbar。 我想将图像置于UINavigationbar上。所以我必须将图像放入UIView width 0。 图像自我获得的x值是自己width的一半。

UINavigationItem *item = navigationController.topViewController.navigationItem;
UIView *backView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];
[img_logo setFrame:CGRectMake(-45, 5, 90, 20)];
[backView addSubview:img_logo];
item.titleView = backView;

答案 2 :(得分:3)

很好的答案!但是,sizeWithFont现已弃用。你现在想要做这样的事情。

NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:  [UIFont fontWithName:@"Helvetica" size:30], NSFontAttributeName, nil];

CGSize requestedTitleSize = [[NSString stringWithFormat:@"Your String"] sizeWithAttributes:textTitleOptions];

CGFloat titleWidth = MIN(self.view.frame.size.width, requestedTitleSize.width);

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleWidth, 44)];

答案 3 :(得分:2)

我使用autolayout约束来解决这个问题:

  1. 将UILabel添加到UIView中。
  2. 为UILabel设置centerX的约束:

    self.titleLabelCenterXConstraint = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];

  3. 在UIView layoutSubViews中调整centerX偏移值:

    - (void)layoutSubviews { [super layoutSubviews]; CGFloat screenCenter = CGRectGetMidX([UIScreen mainScreen].bounds); CGFloat diffCenter = screenCenter - CGRectGetMidX(self.frame); self.titleLabelCenterXConstraint.constant = diffCenter; }

  4. 将UIView设置为navigationItem titleView

答案 4 :(得分:1)

Swift 2.3:

    let tlabel = UILabel(frame: CGRectMake(0, 0, 200, 40))
    tlabel.text = self.title
    tlabel.textColor = UIColor.whiteColor()
    tlabel.font = UIFont.boldSystemFontOfSize(17) //UIFont(name: "Helvetica", size: 17.0)
    tlabel.backgroundColor = UIColor.clearColor()
    tlabel.adjustsFontSizeToFitWidth = true
    tlabel.textAlignment = .Center
    self.navigationItem.titleView = tlabel

答案 5 :(得分:1)

Swift 3 for @ BHuelse的回答:

let image = UIImage(named: "logo")
let logoView = UIView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 30))
let imageView = UIImageView(frame: CGRect(x: -45, y: 5, width: 90, height: 20))
imageView.image = image
imageView.contentMode = .scaleAspectFit
logoView.addSubview(imageView)
self.navigationItem.titleView = logoView

答案 6 :(得分:0)

标题不在导航栏本身的中心。它位于导航栏的左按钮和右按钮之间。这意味着如果左侧有一个大按钮,标题将向右移动。

您可以通过在标题中添加居中约束来更改中心,然后对其进行修改,使其真正成为导航栏的中心:

// Position the title in the middle of the navbar and not in the middle of buttons
fileprivate func centerTitle () {
    if let navigation = self.navigationController {
        let titleMiddle = navigation.navigationBar.convert(titleViewLabel.frame, from: titleViewLabel.superview).midX
        let diff = navigation.navigationBar.center.x - titleMiddle
        constraintTitleViewCentered.constant += diff
    }
}
相关问题