Coder Social home page Coder Social logo

Comments (2)

vicc avatar vicc commented on August 15, 2024 1

@annehiro This is actually fairly easy. Since a gradient color is simply a UIColor, you can use Apple's UIAppearance protocol to do exactly what you want.

Here's a quick example to get you started. In your app's delegate, simply do the following:

    //1. Define the frame for our GradientColor to cover (default navigation bar size)
    CGRect defaultNavigationBarFrame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64);

    //2. Create an array of colors you'd like to use in your gradient
    NSArray *colors = [NSArray arrayWithObjects:FlatYellow, FlatRed, nil];

    //3. Create your gradient color
    UIColor *myGradientColor = [UIColor colorWithGradientStyle:UIGradientStyleTopToBottom
                                                     withFrame:defaultNavigationBarFrame
                                                     andColors:colors];

    //4. Modify the barTintColor for all instances of UINavigationBar
    [[UINavigationBar appearance] setBarTintColor:myGradientColor];

Of course, you could also simplify it down to one line of code:

[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithGradientStyle:UIGradientStyleTopToBottom
                                                                        withFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)
                                                                        andColors:[NSArray arrayWithObjects:FlatYellow, FlatRed, nil]]];

And if that's not short enough, you can use Chameleon Shorthand to shorten it even more.

[[UINavigationBar appearance] setBarTintColor:GradientColor(UIGradientStyleTopToBottom,
                                                                CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64),
                                                                (@[FlatYellow, FlatRed]))];
Note: Don't forget to add a set of parentheses around your color array if using Chameleon Shorthand.

Hopefully this is enough to get you started. Let me know if you have anymore questions. 😃

from chameleon.

vicc avatar vicc commented on August 15, 2024

I'm gonna go ahead and close this, since it seems like you may have figured it out. Feel free to open up a new issue if you're having any other troubles. 👍

from chameleon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.