如何更改UIToolbar按钮的标题颜色?

时间:2013-07-05 10:07:20

标签: ios uitoolbar uiappearance

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

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

5 个答案:

答案 0 :(得分:2)

iOS> = 7.0

UITextAttributeTextColor 已弃用

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

设置UIToolBar的默认标题颜色:

[_myUIToolBar setTintColor:[UIColor redColor]];

或设置单UIBarButtonItem的标题颜色:

[_myUIBarButtonItem setTintColor:[UIColor blueColor]];

答案 1 :(得分: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/检查此链接

答案 2 :(得分: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)

    }

答案 3 :(得分:-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];

答案 4 :(得分:-3)

示例代码:

[UIToolbar appearance]

在文字属性后添加:

enter image description here

这里有一个UIToolBar类引用:

UIToolBar class