简体   繁体   English

UITableView 上下阴影

[英]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.在我的一个观点中,我使用的是 UIWebView,我喜欢背景视图顶部和底部以及滚动区域顶部和底部的默认阴影效果。

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...时钟应用程序中也有相同的内容,具有自定义背景(蓝线与世界地图),因此我猜也可以使用 UITableView...

Here is what I have with my web view, and that I'd like to add to a table view.这是我的 web 视图,我想添加到表视图中。 My web view:我的 web 视图:

在此处输入图像描述

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.这是我添加相同背景的另一个视图的下方,但您可以看到没有阴影......我猜这不是 UIScrollView 的内置效果。 Is there a way to do the same with a table view?有没有办法对表格视图做同样的事情?

My table view:我的表格视图:

在此处输入图像描述

UPDATE 1:更新 1:

I found this great articleUITableView Shadows but there is no shadow for the bottom of the view.我发现这篇很棒的文章UITableView 阴影但视图底部没有阴影。

Here is my code in Swift I think it works well:这是我在 Swift 中的代码,我认为它运行良好:

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)
        }
    }

A fundamental piece of a view is it's layer which is a CALayer.视图的一个基本部分是它的层,它是一个 CALayer。 You can access it through the layer property of a view:您可以通过视图的 layer 属性访问它:

myView.layer

In the documentation CALayers have several properties that control the shadow.在文档中,CALayers 有几个控制阴影的属性。 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.您需要确保将 QuartzCore 框架添加到您的项目中才能访问此属性。

This works for both UITableViewController and UIViewController .这适用于UITableViewControllerUIViewController In my viewDidLoad: I use the following code:在我的viewDidLoad:我使用以下代码:

[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:这是我在 Photoshop 中拼凑的影子:

shadow@2x.png:在此处输入图像描述

My code.我的代码。 Eg for shadow in footer:例如页脚中的阴影:

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,tableView.bounds.size.width, 11)];

    UIImageView *shadow = [[UIImageView alloc] initWithImage:[UIImage 
                                                  imageNamed:@"tableHeaderBottom"]];
    [shadow sizeToFit];
    [footerView addSubview:shadow];

    return footerView;
}

- (void)viewDidLoad
{
    [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, -11, 0)];
    [super viewDidLoad];
}

The images:图像:
正常图像视网膜图像

For the header: Use the same code but just flip the image vertically and use viewForHeaderInSection on the first section (0).对于 header:使用相同的代码,但只需垂直翻转图像并在第一部分 (0) 上使用viewForHeaderInSection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM