Issues setting gradient image as UINavigationBar background

Kex

I'm trying to set a gradient image background for my navigation bar. Code is like so:

extension CAGradientLayer {

    class func gradientLayerForBounds(bounds: CGRect, startColor: UIColor, endColor: UIColor) -> CAGradientLayer {
        let layer = CAGradientLayer()
        layer.frame = bounds
        layer.colors = [startColor.CGColor, endColor.CGColor]
        return layer
    }
}

Then in my view controller:

func imageLayerForGradientBackground() -> UIImage{

        var updatedFrame = self.navigationController!.navigationBar.bounds
        updatedFrame.size.height += 20 //Status bar
        let layer = CAGradientLayer.gradientLayerForBounds(updatedFrame, startColor: UIColor(hexString: "5FD238")!, endColor: UIColor(hexString: "A5E589")!)
        UIGraphicsBeginImageContext(layer.bounds.size)
        layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image

    }

viewDidLoad:

   navigationController!.navigationBar.translucent = false
   navigationController!.navigationBar.tintColor = UIColor.whiteColor()
    navigationController!.navigationBar.setBackgroundImage(imageLayerForGradientBackground(), forBarMetrics: UIBarMetrics.Default)

The issue is that all my views under the navigation bar are now pushed down by what look like the height of the navigation bar (as drawn badly in the diagram below):

enter image description here

Why is this happening? Any pointers on this would be greatly appreciated! Thanks!

EDIT:

func setupDrawerView() {

        answerDrawerView = AnswerDrawerView(frame: CGRect(x: 0, y: UIScreen.mainScreen().bounds.size.height - AnswerDrawerView.submitTabHeight, width: UIScreen.mainScreen().bounds.size.width, height: UIScreen.mainScreen().bounds.height*0.75))
        answerDrawerView.delegate = self
        view.addSubview(answerDrawerView)
    }
matt

This has nothing whatever to do with the gradient. It's all about this line:

navigationController!.navigationBar.translucent = false

As soon as you say that, the main view stops underlapping the navigation bar. Well, you've positioned your views absolutely instead of using auto layout, so since the Biology button was, let's say, 35 points down from the top of the main view, it is still 35 points down from the top of the main view — but the top of the main view is now at the bottom of the navigation bar. Hence the gap.

The moral: use auto layout! This kind of situation is exactly what it is for. Position your views in relation to the top layout guide, not the top of the main view, and all will be well.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

setting a linear gradient above a background image in Bootstrap

From Dev

Customize UINavigationBar in IOS without using UINavigationController setting background image not working

From Dev

UINavigationBar + iOS7 background image not setting properly

From Dev

Swift - Issues with setting background image in a UIScrollView

From Dev

Setting background-image (linear gradient) using jquery variables

From Dev

Why the UINavigationBar Background Image is Repeating?

From Dev

uinavigationbar background image hides title

From Dev

UINavigationBar background image with iPhone 6

From Dev

iOS 8 UINavigationBar Background Image

From Dev

iOS 8 UINavigationBar Background Image

From Dev

CSS : Background Image with Background Gradient

From Dev

Setting the same background colour on UIStatusBar as UINavigationBar

From Dev

Background image issues

From Dev

Background-image gradient not working

From Dev

Linear gradient and background image on a button

From Dev

Create a gradient around a background image

From Dev

Background-image with gradient effect

From Dev

Setting background color and background image

From Dev

Setting image as Background

From Dev

Setting background color after setting background image

From Dev

Setting background color instead of gradient for older browsers

From Dev

Setting background color instead of gradient for older browsers

From Dev

Setting background gradient with jquery - jshint error

From Dev

Background-image with background-gradient

From Dev

CSS - Background gradient and background image sent to bottom

From Dev

Having issues with background image of a section

From Dev

Setting MdiParent Background Image properly

From Dev

Setting image view background to bitmap

From Dev

Setting background image on dynamic element

Related Related

  1. 1

    setting a linear gradient above a background image in Bootstrap

  2. 2

    Customize UINavigationBar in IOS without using UINavigationController setting background image not working

  3. 3

    UINavigationBar + iOS7 background image not setting properly

  4. 4

    Swift - Issues with setting background image in a UIScrollView

  5. 5

    Setting background-image (linear gradient) using jquery variables

  6. 6

    Why the UINavigationBar Background Image is Repeating?

  7. 7

    uinavigationbar background image hides title

  8. 8

    UINavigationBar background image with iPhone 6

  9. 9

    iOS 8 UINavigationBar Background Image

  10. 10

    iOS 8 UINavigationBar Background Image

  11. 11

    CSS : Background Image with Background Gradient

  12. 12

    Setting the same background colour on UIStatusBar as UINavigationBar

  13. 13

    Background image issues

  14. 14

    Background-image gradient not working

  15. 15

    Linear gradient and background image on a button

  16. 16

    Create a gradient around a background image

  17. 17

    Background-image with gradient effect

  18. 18

    Setting background color and background image

  19. 19

    Setting image as Background

  20. 20

    Setting background color after setting background image

  21. 21

    Setting background color instead of gradient for older browsers

  22. 22

    Setting background color instead of gradient for older browsers

  23. 23

    Setting background gradient with jquery - jshint error

  24. 24

    Background-image with background-gradient

  25. 25

    CSS - Background gradient and background image sent to bottom

  26. 26

    Having issues with background image of a section

  27. 27

    Setting MdiParent Background Image properly

  28. 28

    Setting image view background to bitmap

  29. 29

    Setting background image on dynamic element

HotTag

Archive