2

是否有任何简单的方法可以更改 UIToolBar 按钮标题的标题颜色,就像我们用于 UIBarButtonItem 的方法一样:

[[UIBarButtonItem appearance] 
    setTitleTextAttributes:
        [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], 
            UITextAttributeTextColor,nil] 
    forState:UIControlStateNormal];
4

5 回答 5

2
+(UIBarButtonItem*)barButtonItemWithTint:(UIColor*)color andTitle:(NSString*)itemTitle andTarget:(id)theTarget andSelector:(SEL)selector
{
    UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:itemTitle, nil]] autorelease];
    button.momentary = YES;
    button.segmentedControlStyle = UISegmentedControlStyleBar;
    button.tintColor = color;
    [button addTarget:theTarget action:selector forControlEvents:UIControlEventValueChanged];

    UIBarButtonItem *removeButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

    return removeButton;
}

使用此代码或http://fredandrandall.com/blog/2011/03/31/how-to-change-the-color-of-a-uibarbuttonitem/检查此链接

于 2014-01-29T11:47:44.783 回答
2

UITextAttributeTextColoriOS >= 7.0中已弃用

setTintColor:(UIColor *)改为使用更改按钮标题的颜色。

设置默认标题颜色UIToolBar

[_myUIToolBar setTintColor:[UIColor redColor]];

或设置单一的标题颜色UIBarButtonItem

[_myUIBarButtonItem setTintColor:[UIColor blueColor]];
于 2013-12-04T17:26:25.423 回答
2

通通APP:

UIToolbar.appearance().barTintColor = TOOLBAR_BACKGROUND_COLOR
UIToolbar.appearance().tintColor = TOOLBAR_TITLE_COLOR 

 if let font = UIFont(name: "AvenirNext-DemiBold", size: 15) {
        UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName:TOOLBAR_TITLE_COLOR], forState: UIControlState.Normal)

    }
于 2015-11-18T06:45:00.137 回答
-2

//Best Way.. 尝试为 UIBarButtonItem.. 设置图像

UIButton *a1 = [UIButton buttonWithType:UIButtonTypeCustom];
[a1 setFrame:CGRectMake(0.0f, 0.0f, 18.0f, 18.0f)];
[a1 addTarget:self action:@selector(BarButton_Clicked:) forControlEvents:UIControlEventTouchDown];
[a1 setImage:[UIImage imageNamed:@"UnSelected.png"] forState:UIControlStateNormal];
[a1 setImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];
UIBarButtonItem *btn_Image = [[UIBarButtonItem alloc] initWithCustomView:a1];

UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
[flexibleSpace setWidth:25.0];

self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:flexibleSpace,btn_Image,nil];
于 2013-07-05T10:33:38.177 回答
-3

这里的示例代码:

[UIToolbar appearance]

在您的文本属性之后添加:

在此处输入图像描述

这里有一个 UIToolBar 类参考:

UIToolBar 类

于 2013-07-05T10:23:03.417 回答