我的解决方案:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(changeMapViewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 50, 30)];
[button setTitle:@"3D" forState:UIControlStateNormal];
[button setTitleColor:self.view.tintColor forState:UIControlStateNormal];
[button setTitle:@"3D" forState:UIControlStateHighlighted];
[button setTitleColor:[self.view.tintColor colorWithAlphaComponent:0.15] forState:UIControlStateHighlighted];
[button.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Thin" size:25]];
UIBarButtonItem *changeMapViewItem = [[UIBarButtonItem alloc] initWithCustomView:button];
这是它的样子:
我试过了:
UIBarButtonItem *changeMapViewItem = [[UIBarButtonItem alloc] initWithTitle:@"3D" style:UIBarButtonItemStylePlain target:self action:@selector(changeMapViewButtonPressed:)];
[changeMapViewItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaNeue-Thin" size:25], NSFontAttributeName,nil] forState:UIControlStateNormal];
但随后文本与 UIToolbar 中的图标不对齐。
HTH 多米尼克