UIToolbar setBackgroundColor doesn't fully change color

廉价感情. 提交于 2019-12-03 05:29:00

问题


I'm trying to set the background color of a UIToolBar. I tried selecting the color from IB's Attribute Inspector, and tried setting it programmatically through setBackgroundColor:[UIColor ...].

Both solutions work, but only partially: the color blends something like 50% with white and the toolbar is very light...doesn't show the color I actually chose, but a much lighter version of it.

How can I have the UIToolBar of the actual color I'm choosing? It's probably very simple to solve, but I can't find a way and can't find answers online either.


回答1:


Write below code in your viewDidLoad

self.navigationController.toolbar.barTintColor = [UIColor redColor];

It will set red color as your tool bar background.

Reference link https://web.archive.org/web/20160321155823/https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/Bars.html#//apple_ref/doc/uid/TP40013174-CH8-SW5

In it they said that Use barTintColor to tint the bar background.




回答2:


IN iOS 7 you need to set the barTintColor Property-

UIToolbar *doneToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 584, 320, 44)];
doneToolbar.translucent=NO;
doneToolbar.barTintColor=[UIColor redColor];
[self.view addSubview:doneToolbar];

I have used it its working fine...




回答3:


In addition to Jageen's answer, you must also set the translucent property to false. Otherwise the color will have slightly less saturation and hue than what is specified with barTintColor.

// Sets to a specific color
self.navigationController.toolbar.barTintColor = UIColor colorWithRed:6.0 / 255.0 green:52.0 / 255.0 blue:90.0 / 255.0 alpha:1.0];

// Without this, color will be faded slightly and not exactly what's specified above  
self.navigationController.toolbar.translucent = false;



回答4:


Try this on IOS 10:

let dummyToolbar = UIToolbar()
dummyToolbar.barTintColor = .lightGray
dummyToolbar.sizeToFit() // without this line it doesn't work



回答5:


UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];

numberToolbar.backgroundcolor = [UIColor redcolor]; numberToolbar.items = [NSArray arrayWithObjects: [[UIBarButtonItem alloc]initWithTitle:@"Clear" style:UIBarButtonItemStyleBordered
nil];

[numberToolbar sizeToFit];
numberTextField.inputAccessoryView = numberToolbar;



回答6:


Throughout App:

    UIToolbar.appearance().barTintColor = TOOLBAR_BACKGROUND_COLOR

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

    }



回答7:


Swift 4+:

toolBar.barTintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
toolBar.isTranslucent = false
toolBar.sizeToFit()


来源:https://stackoverflow.com/questions/19401507/uitoolbar-setbackgroundcolor-doesnt-fully-change-color

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!