UIToolbar setBackgroundColor doesn't fully change color

帅比萌擦擦* 提交于 2019-12-02 19:56:26
Jageen

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.

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...

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;

Try this on IOS 10:

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

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;

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)

    }

Swift 4+:

toolBar.barTintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
toolBar.isTranslucent = false
toolBar.sizeToFit()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!