UITableView阴影顶部和底部

时间:2011-06-09 21:23:06

标签: iphone uitableview uiscrollview gradient

在我的一个视图中,我使用的是UIWebView,我喜欢背景视图顶部和底部的默认阴影效果,以及滚动区域的顶部和底部。

时钟应用程序中有相同的自定义背景(带世界地图的蓝线)所以可以使用UITableView我猜...

以下是我在网络视图中的内容,并且我想添加到表格视图中。我的网络视图:

enter image description here

所以我在这里添加了自定义背景(浅灰色纹理)。这是在我添加相同背景的另一个视图下方,但正如您所看到的那样没有阴影......我猜这不是UIScrollView的内置效果。有没有办法对表视图做同样的事情?

我的表格视图:

enter image description here

更新1:

我找到了这篇很棒的文章UITableView Shadows,但视图的底部没有阴影。

4 个答案:

答案 0 :(得分:3)

视图的一个基本部分是它的层是CALayer。您可以通过视图的图层属性访问它:

myView.layer

在文档中,CALayers有几个控制阴影的属性。您应该能够告诉导航栏的图层投射阴影。与您想要投射阴影的任何其他内容的图层一起。

myView.layer.shadowOffset
myView.layer.shadowRadius

您需要确保将QuartzCore框架添加到项目中以访问此属性。

答案 1 :(得分:3)

这是我在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)
        }
    }

答案 2 :(得分:2)

这适用于UITableViewControllerUIViewController。 在我的viewDidLoad:中,我使用以下代码:

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

这将在导航栏下添加阴影。

这是我在Photoshop中投入的阴影:

shadow@2x.png: enter image description here

答案 3 :(得分:0)

我的代码。例如。对于页脚中的阴影:

- (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];
}

图像:
Normal image Retina image

对于标题:使用相同的代码但只是垂直翻转图像并在第一部分(0)上使用viewForHeaderInSection