1

AppDelegate.m 导航栏自定义:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes: @{
                            UITextAttributeTextColor: [UIColor colorWithRed:(56/255.0) green:(61/255.0) blue:(63/255.0) alpha:1],
                            UITextAttributeTextShadowColor: [UIColor grayColor],
                            UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
                            UITextAttributeFont: [UIFont fontWithName:@"ProximaNova-Bold" size:15.0f]
 }];

LViewController.m 导航栏自定义左/右按钮:

// Left Button
UIButton *leftButton =  [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setImage:[UIImage imageNamed:@"menu"] forState:UIControlStateNormal];
[leftButton addTarget:self action:@selector(openLeftMenu) forControlEvents:UIControlEventTouchUpInside];
[leftButton setFrame:CGRectMake(0, 0, 45, 44)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];

// Right Button
UIButton *rightButton =  [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setImage:[UIImage imageNamed:@"location"] forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(openRightMenu) forControlEvents:UIControlEventTouchUpInside];
[rightButton setFrame:CGRectMake(0, 0, 45, 44)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];

结果(标题不居中):

标题不居中

我尝试了很多技巧,但有人工作:

4

1 回答 1

0

当涉及到文本的垂直方向时,在 iOS 上使用自定义字体充满了危险。您最好的选择是将标题嵌入 a 中UILabel,然后将其放置UILabel在 a 中UIView,适当定位。像这样的东西:

UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = @"Test";

UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0.0f,
                                                             0.0f,
                                                             320.0f,
                                                             44.0f)];

titleLabel.frame = …; // Compute the frame you need based on the chosen font, set
                      // appropriately.

[container addSubview:titleLabel];

self.navigationItem.titleView = container;
于 2013-08-24T02:06:58.533 回答