2013-08-23 174 views
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]; 

结果(标题不居中):

Title not Centered

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

+0

那是“自定义视图”或“NSString”直线的“HOME”标题吗? – yeesterbunny

回答

0

当涉及到文本的垂直方向时,在iOS上处理自定义字体充满了危险。这里最好的选择是将标题嵌入UILabel,然后将UILabel置于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;