Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Shadows top and bottom

In one of my view I'm using a UIWebView and I like the default shadow effects on top and bottom of the background view, and also top and bottom of the scroll area.

There is the same in the Clock app, with a custom background (blue line with world map) so it is possible using a UITableView also I guess...

Here is what I have with my web view, and that I'd like to add to a table view. My web view:

enter image description here

So I added an custom background here (the light gray texture). Here is below the other view where I added the same background, but as you can see there is no shadow... It's not a built-in effect as a UIScrollView I guess. Is there a way to do the same with a table view?

My table view:

enter image description here

UPDATE 1:

I found this great article UITableView Shadows but there is no shadow for the bottom of the view.

like image 428
Dachmt Avatar asked Jun 09 '11 21:06

Dachmt


3 Answers

A fundamental piece of a view is it's layer which is a CALayer. You can access it through the layer property of a view:

myView.layer

In the documentation CALayers have several properties that control the shadow. You should be able to tell the navigation bar's layer to cast a shadow. Along with the layers of any other content that you want to cast a shadow.

myView.layer.shadowOffset
myView.layer.shadowRadius

You will need to be sure to add the QuartzCore framework to your project to access this property.

like image 184
Dancreek Avatar answered Nov 16 '22 09:11

Dancreek


Here is my code in Swift I think it works well :

func shadowView (view : UIView , r : CGFloat, gr : CGFloat , b : CGFloat , header : Bool)
    {
        let bottomColor = UIColor (red: r/255 , green: gr/255 , blue: b/255 , alpha: 0)

        //var bottomColor = UIColor ( red: (r/255), green : (g/255), blue : (b/255), alpha : 0)
        //var topColor = UIColor ( red: (r/255), green : (g/255), blue : (b/255), alpha : 1)
        let topColor = UIColor (red: r/255, green: gr/255, blue: b/255, alpha: 1)
        var arrayColors: [CGColor] = [
            topColor.CGColor,
            bottomColor.CGColor
        ]

        if header
        {
            let headerShadow: CAGradientLayer = CAGradientLayer()

            headerShadow.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: view.frame.size.height)
            headerShadow.colors = arrayColors
            view.layer.insertSublayer(headerShadow, atIndex: 1)
        } else
        {
            let footerShadow: CAGradientLayer = CAGradientLayer()

            arrayColors  = [
                bottomColor.CGColor,
                topColor.CGColor
            ]
            footerShadow.frame = CGRect(x: 0, y: 0 , width: UIScreen.mainScreen().bounds.width, height: view.frame.size.height)
            footerShadow.colors = arrayColors
            view.layer.insertSublayer(footerShadow, atIndex: 1)
        }
    }
like image 3
Alaa Avatar answered Nov 16 '22 10:11

Alaa


This works for both UITableViewController and UIViewController. In my viewDidLoad: I use the following code:

[self.view addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shadow.png"]]];

This will add the shadow just under the navigation bar.

And this is my shadow I threw together in Photoshop:

[email protected]: enter image description here

like image 2
WrightsCS Avatar answered Nov 16 '22 10:11

WrightsCS