Monday, 17 July 2017

Add gradient color as bar tint color in UINavigationBar

To set the background color of UINavigation bar , we set the barTintColor property of UINavigationBar.
        self.navigationController?.navigationBar.barTintColor = YOUR_COLOR

But what if you want to set the gradient color as tint color. For this we can take either create a CAGradientLayer with desired color and then render that layer in current graphics context and get the  image from the current context and set backgroundImage of the UINavigationBar.

Second we can draw a gradient image using core graphics without creating a CAGradientLayer and then set the backgroundImage of UINavigationBar.

Here we are going with second one, using core graphics. Swift provide us many functionalities to write reusable code so that we can use it anywhere in our application.


extension UIColor {
    convenience init(r: Float, g: Float, b: Float,alpha:Float) {
        self.init(colorLiteralRed: r/255, green: g/255, blue: b/255, alpha: alpha)
    }
}


extension UINavigationBar{
    
    func gradeintBarTintColor(with gradient:[UIColor]){
        var frameAndStatusBarSize = self.bounds
        frameAndStatusBarSize.size.height +=  20
        setBackgroundImage(UINavigationBar.gradient(size: frameAndStatusBarSize.size, color: gradient), for: .default)
    }
    
    static func gradient(size:CGSize,color:[UIColor]) -> UIImage?{
        //turn color into cgcolor
        let colors = color.map{$0.cgColor}
        
        //begin graphics context
        UIGraphicsBeginImageContextWithOptions(size, true, 0.0)
        guard let context = UIGraphicsGetCurrentContext() else {
            return nil
        }
        
        // From now on, the context gets ended if any return happens
        defer {UIGraphicsEndImageContext()}
        
        //create core graphics context
        let locations:[CGFloat] = [0.0,1.0]
        guard let gredient = CGGradient.init(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors as NSArray as CFArray, locations: locations) else {
            return nil
        }
        //draw the gradient
        context.drawLinearGradient(gredient, start: CGPoint(x:0.0,y:0.0), end: CGPoint(x:0.0,y:size.height), options: [])
        
        // Generate the image (the defer takes care of closing the context)
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}




Usage:

Here I added gradient from top to bottom, you can change the implementation as per your requirements.
In your view controller, use like this:
        
//add gradeint color as bartint color
        
let colors = [UIColor(r: 73.0, g: 183.0, b: 229.0, alpha: 1.0),UIColor(r: 44.0, g: 154.0, b: 202.0, alpha: 1.0)]
        self.navigationController?.navigationBar.gradeintBarTintColor(with: colors)

Results:





References:

19 comments:


  1. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.

    AWS Online Training | Online AWS Certification Course - Gangboard
    AWS Training in Chennai | AWS Training Institute in Chennai Velachery, Tambaram, OMR
    AWS Training in Bangalore |Best AWS Training Institute in BTM ,Marathahalli

    ReplyDelete
  2. it is not working for large title naviigation bar

    ReplyDelete
  3. It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

    python training Course in chennai | python training in Bangalore | Python training institute in kalyan nagar

    ReplyDelete
  4. This is such a good post. One of the best posts that I\'ve read in my whole life. I am so happy that you chose this day to give me this. Please, continue to give me such valuable posts. Cheers!

    Java training in Chennai | Java training in USA |

    Java training in Indira nagar | Java training in Rajaji nagar

    ReplyDelete
  5. I appreciate that you produced this wonderful article to help us get more knowledge about this topic. I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!
    Data Science training in kalyan nagar | Data Science training in OMR

    Data Science training in chennai | Data science training in velachery

    Data science training in tambaram | Data science training in jaya nagar

    ReplyDelete
  6. Thanks for your great and helpful presentation I like your good service.I always appreciate your post.That is very interesting I love reading and I am always searching for informative information like this.Well written article Thank You for Sharing with Us project management courses in chennai | pmp training class in chennai | pmp training fee | project management training certification | project management training in chennai | project management certification online

    ReplyDelete
  7. Appreciating the persistence you put into your blog and detailed information you provide.

    Data science Course Training in Chennai |Best Data Science Training Institute in Chennai

    ReplyDelete
  8. Your new valuable key points imply much a person like me and extremely more to my office workers. With thanks; from every one of us.
    Aws training chennai | AWS course in chennai
    Rpa training in chennai | RPA training course chennai
    oracle training chennai | oracle training in chennai

    ReplyDelete
  9. Thank you for sharing your wonderful information. for professional graphics,
    work contacts me. I am a freelance designer in gurgaon.
    Freelance Graphic Designing
    Freelance Catalogue Designing in delhi
    Freelance Catalogue Designing in gurgaon
    Freelance Brochure Designing
    Freelance Label Designing
    Freelance Banner Designer
    Freelance Poster Designer
    graphic design services in delhi
    graphic design services in gurgaon
    Freelance Catalogue Designing in delhi
    Freelance Catalogue Designing in gurgaon
    Freelance Brochure Designing
    Freelance Label Designing
    Freelance Banner Designer
    Freelance Poster Designer
    graphic design services in delhi
    graphic design services in gurgaon
    freelance website designer in gurgaon
    freelance designer in gurgaon
    freelance website designer in gurgaon
    freelance web designer in gurgaon
    freelance graphic designer services in gurgaon
    freelancer graphic designer services in gurgaon
    freelancer graphic designer services in gurgaon
    freelancer graphic services in gurgaon
    freelancer logo services in gurgaon
    freelancer logo services in gurgaon
    freelancer web designer services in gurgaon
    freelancer web designer services in gurgaon
    freelance web designer services in gurgaon
    freelance website designer services in gurgaon
    freelance website designer services in gurgaon
    freelance logo designer service in gurgaon
    freelance logo designer service in gurgaon
    logo designer in gurgaon
    brochure design in gurgaon
    logo design in gurgaon
    freelance logo design in gurgaon
    freelance logo designer in gurgaon
    freelance logo designer in gurgaon

    ReplyDelete
  10. I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here
    Allahabad BCom 3rd Year Time Routine 2020
    Delhi University BCOM 2nd Year Time Schedule 2020
    MDSU BCOM 1st Year TimeTable/Date Sheet 2020

    ReplyDelete
  11. Hi there colleagues, good paragraph and good urging commented here, I am genuinely enjoying by these. KU BA Part 1 Result

    ReplyDelete
  12. Did you want to set your career towards Big Data? Then Infycle is with you to make this into your life. Infycle Technologies gives the combined and best Big Data Hadoop training in Chennai, along with the 100% hands-on training guided by professional teachers in the field. In addition to this, the mock interviews for the placement will be guided to the candidates, so that, they can face the interviews with full confidence. Once after the mock interview, the candidates will be placed in the top MNC's with a great salary package. To get it all, call 7502633633 and make this happen for your happy life.Big Data Hadoop Training in Chennai | Infycle Technologies

    ReplyDelete
  13. This site helps to clear your all query. dbrau ba 3rd year result
    bu ba final year result This is really worth reading. nice informative article.

    ReplyDelete
  14. साझा की गई उपयोगी जानकारी के लिए धन्यवाद। b a third year exam date लेख वास्तव में बहुत मददगार है।

    ReplyDelete