UITableView 顶部和底部阴影

发布于 2024-11-14 15:31:32 字数 624 浏览 3 评论 0原文

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

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

这是我的网络视图,我想添加到表视图。我的网页视图:

在此处输入图像描述

所以我在此处添加了自定义背景(浅灰色纹理)。这是另一个视图的下面,我添加了相同的背景,但正如你所看到的,没有阴影......我猜它不是 UIScrollView 的内置效果。有没有办法对表视图执行相同的操作?

我的表格视图:

在此处输入图像描述

更新 1:

我发现这篇很棒的文章 UITableView 阴影 但没有阴影视图的底部。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不必你懂 2024-11-21 15:31:32

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

myView.layer

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

myView.layer.shadowOffset
myView.layer.shadowRadius

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

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.

白芷 2024-11-21 15:31:32

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

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)
        }
    }
美人迟暮 2024-11-21 15:31:32

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

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

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

这是我在 Photoshop 中拼凑而成的影子:

[email protected]:
在此处输入图像描述

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

吾性傲以野 2024-11-21 15:31:32

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

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

图像:
普通图像
Retina image

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

My code. E.g. 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:
Normal image
Retina image

For the header: Use the same code but just flip the image vertically and use viewForHeaderInSection on the first section (0).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文