Set a simple color gradient as the background of UIToolbar

swift change button background color when pressed
uibutton background color not changing swift
uibutton set background color
swift change button color when pressed
swift uibutton background color
swift highlight button when pressed
uibutton set selected state programmatically
uibutton selected and unselected swift

I can easily change the UIToolbar background color thanks to the Bart Tint property in Interface Builder. However, I would like to use a simple, two-color vertical gradient as background.

The solution should work with any custom toolbar height, and my target is iOS 7+. If it must be done programmatically, both Objective-C and Swift are welcome.

Edit: based on the answers I'm doing this in viewDidLoad, but it makes no effect:

var gradient = CAGradientLayer()
gradient.frame = toolbar.bounds
gradient.colors = [UIColor.redColor(), UIColor.greenColor()]
toolbar.layer.insertSublayer(gradient, atIndex: 0)

Edit 2: based on the answers, I replaced the following line, but it still makes no effect:

gradient.colors = [UIColor.redColor().CGColor, UIColor.greenColor().CGColor]

Edit 3: I got it. I had to set "Bar Tint" to "Default" in Interface Builder first, so that the tint is transparent and doesn't hide the programmatically added gradient layer.

You can use CAGradientLayer for achieving this.

Objective C:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame            = yourToolbar.bounds;
gradient.colors           = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor], (id)[[UIColor greenColor] CGColor], nil];
[yourToolbar.layer insertSublayer:gradient atIndex:0];

Swift:

var gradient:CAGradientLayer = CAGradientLayer()
gradient.frame               = yourToolbar.bounds
gradient.colors              = [UIColor.redColor().CGColor, UIColor.greenColor().CGColor]
yourToolbar.layer.insertSublayer(gradient, atIndex: 0)

ios Set a simple color gradient as the background of UIToolbar , ios Set a simple color gradient as the background of UIToolbar? var gradient:​CAGradientLayer = CAGradientLayer() gradient.frame = self.toolbar.bounds  The quickest way to add a background to an element is to add a single-color background using the background or background-color property. The background property accepts colors and images in shorthand form, while the background-color property is used strictly for setting solid background colors. Either property will work, and which one you decide to use depends on your preference as well as the case for which you’re using it.

This works

    var gradient:CAGradientLayer = CAGradientLayer()
    gradient.frame = self.toolbar.bounds
    gradient.colors = [UIColor.redColor().CGColor, UIColor.greenColor().CGColor]
    self.toolbar.layer.insertSublayer(gradient, atIndex: 0)

Gradient Backgrounds Studio: Lessons Learned, Gradient Backgrounds Studio is a very simple app with a very simple The first thing I had to learn was how to create and manipulate gradients. class draws a color gradient over its background color, filling the shape of the layer at people suggesting to use UIToolbar , and looked into solutions like  Background Color Example. The background color property allows you to choose the color of your element. This can be the background for the entire page or the background of one section of your page. An element is a piece of HTML such as a header or paragraph on a web page. Here is an example of setting the background color for a web page to green.

It's the same way as setting a Gradient Background for UINavigationBar - you can use the setBackgroundImage method of UIToolbar.

Just add these two extensions:

extension UIImage {
    static func imageFromLayer (layer: CALayer) -> UIImage? {
        UIGraphicsBeginImageContext(layer.frame.size)
        guard let currentContext = UIGraphicsGetCurrentContext() else {return nil}
        layer.render(in: currentContext)
        let outputImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return outputImage
    }
}

extension UIToolbar {
    func setGradientBackground(barFrame: CGRect, aColor: UIColor, bColor: UIColor, aX: CGFloat, bX: CGFloat, aY: CGFloat, bY: CGFloat) {
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = frame
        gradientLayer.colors = [aColor.cgColor, bColor.cgColor]
        gradientLayer.locations = [0.0, 1.0]
        gradientLayer.startPoint = CGPoint(x: aX, y: aY)
        gradientLayer.endPoint = CGPoint(x: bX, y: bY)
        if let image = UIImage.imageFromLayer(layer: gradientLayer) {
            self.setBackgroundImage(image, forToolbarPosition: .bottom, barMetrics: .default)
        }
    }
}

This creates a UIImage from a CAGradientLayer, and subsequently sets it as a background image of your UIToolbar. Then just apply it to your UIToolbar. For instance, a horizontal gradient from blue to white:

myToolbar.setGradientBackground(
                barFrame: navigationBar.frame,
                aColor: UIColor.blue,
                bColor: UIColor.white,
                aX: 0.0,
                bX: 1.0,
                aY: 0.5,
                bY: 0.5
            )

Hope this helps!

Extending UIButton in Swift with Background Color for State, Swift Tool Belt, Part 8: Extending UIButton with Background Color for you the ability to set the background color for different button states. Many of the apps that I have created lately use flat-button design with a solid color background. Extending UIBarButtonItem; Extending UIButton with Background  CSS Linear Gradients. To create a linear gradient you must define at least two color stops. Color stops are the colors you want to render smooth transitions among. You can also set a starting point and a direction (or an angle) along with the gradient effect.

// Use this Code:

i=2;

        leftColor = [UIColor colorWithRed:0.03 green:0.20 blue:0.95 alpha:1.0];
rightColor = [UIColor colorWithRed:0.98 green:0.00 blue:0.89 alpha:1.0];
gradient.frame = self.font_imageview.bounds;
gradient.colors = [NSArray arrayWithObjects:
                   (id)[rightColor CGColor],
                   (id)[UIColor clearColor].CGColor,
                   (id)[leftColor CGColor],nil];

[font_imageview.layer insertSublayer:gradient atIndex:0];

gradient.startPoint = CGPointMake(0.0, 0.5);
gradient.endPoint = CGPointMake(1.0, 0.5);}

barTintColor, This color is made translucent by default unless you set the isTranslucent property to false . See Also. Changing the Background. Previously I have shared a random gradient generator, but this is a simple moving gradient for background or on any element. Basically, a gradient is a mix of more than one color, that we can easily create using CSS for websites.

If you are using custom toolbar then maybe you face the size problem. view_toolbar can be any view, Here I found another perfect solution:

let gradientLayer = CAGradientLayer()
var bounds =  self.view_toolbar.bounds
bounds.size.height += UIApplication.shared.statusBarFrame.size.height
gradientLayer.frame = bounds
gradientLayer.colors = [colorPrimary, colorPrimaryDark]
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 1, y: 0)

self.view_toolbar.layer.insertSublayer(gradientLayer, at: 0)

Swift set view background image programmatically, To do that, set the cell or the content view's background color to a partly transparent you set the view's background color in viewDidLoad : 3 Apr 2017 let gradient Apr 18, 2019 · To load an image in iOS using swift we'll make use of simple data You send the setter messages to the appearance proxy ([UIBar Button Item  First thing you should know is that there are 3 types of HTML colors: Hex color codes, HTML color names and RGB values. To choose your preferred color use our Color Pickertool. Let’s start from the easiest way of setting a background color: 1. Add the background-color Property to the <body> Element.

CSS Gradients, Set a simple color gradient as the background of UIToolbar. I can easily change the UIToolbar background color thanks to the Bart Tint property in Interface  Magic Gradients is a NuGet that provide a possibility to add beautiful and enhanced gradients into Xamarin.Forms applications. This NuGet package supports all common Xamarin platforms, such as Android, iOS and UWP. It is built upon SkiaSharp to draw a simple multi-color or multi-layer engaged gradients in Skia Surface ( SKSurface ).

Basic usage of Kendo UI ToolBar Widget, Well organized and easy to understand Web building tutorials with lots of examples of how to use To create a linear gradient you must define at least two color stops. background-image: linear-gradient(direction, color-stop1, color-​stop2, .

SASS Theme Builder. Create new or customize SASS themes. Launch Theme Builder · Components · ToolBar · Basic usage · Edit in Theme Builder 

Comments
  • I'm using Swift and cannot find any "layer" under "CAGradientLayer", and I cannot find that "layer" property (I'm referring to the first line of your code)
  • Replaced it with var gradient = CAGradientLayer(). Compiles but still doesn't work.
  • No, but it compiles so that's not the issue. Anyway I tried to import it just in case and the result is the same.
  • Have you tried your code with a UIToolbar in iOS 7+ or this is just a general solution? Because for some reason it's not working for me.
  • @cprcrack: issue in your code, you need to convert it to CGColor, copy my answer and check
  • I tried it (including the .CGColor), and it doesn't seem to work.
  • Have you tried your code with a UIToolbar in iOS 7+ or this is just a general solution? Because for some reason it's not working for me.
  • I am trying it on iOS 8.1 simulator. No problem. Just a barebone project with UIToolbar.
  • I got it. I had to set "Bar Tint" to "Default" in Interface Builder first, so that the tint is transparent and doesn't hide the programmatically added gradient layer.