UITableView背景图片

发布于 2024-11-03 15:57:49 字数 396 浏览 0 评论 0原文

我正在尝试设置一个 png 图像作为我的桌面视图的背景。使用以下代码一切都很好!但仅限于 iPhone 模拟器。如果我尝试在 iPhone 设备上运行该应用程序,表格视图的背景将保持白色(或透明)。你认为这与我尝试设置背景颜色的方式有关吗?我一直在尝试很多方法,直到出现以下问题,但都有同样的问题。

self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableViewBackground.png"]];

先感谢您!

I'am trying to setup a png image as my tableview's background. With the following code all are fine! But only on iPhone Simulator. If I try to run the application on an iPhone device the background of tableview remains white (or clear). Do you thing thing that is something about the way I tried to set the background color. I have been trying many ways until the following, but all have the same issue.

self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableViewBackground.png"]];

Thank you in advance!

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

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

发布评论

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

评论(12

信仰 2024-11-10 15:57:49

请使用以下代码。

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableViewBackground.png"]];
[tempImageView setFrame:self.tableView.frame]; 

self.tableView.backgroundView = tempImageView;
[tempImageView release];

谢谢,

Please use following code.

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableViewBackground.png"]];
[tempImageView setFrame:self.tableView.frame]; 

self.tableView.backgroundView = tempImageView;
[tempImageView release];

Thanks,

菩提树下叶撕阳。 2024-11-10 15:57:49

我认为这种方法更干净:

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"TableBackground.jpg"]];
self.tableView.backgroundColor = background;
[background release];

I think this approach cleaner:

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"TableBackground.jpg"]];
self.tableView.backgroundColor = background;
[background release];
独自唱情﹋歌 2024-11-10 15:57:49

简单的快速版本将是

let tempImageView = UIImageView(image: UIImage(named: "yourImage"))
tempImageView.frame = self.tableView.frame
self.tableView.backgroundView = tempImageView;

Simple swift version will be

let tempImageView = UIImageView(image: UIImage(named: "yourImage"))
tempImageView.frame = self.tableView.frame
self.tableView.backgroundView = tempImageView;
楠木可依 2024-11-10 15:57:49

您的图像实际上名为 TableViewBackground.PNG (注意大写字母)吗?您需要让大小写在 iOS 设备上完全匹配,而在模拟器中则不需要完全匹配。

Is your image actually named TableViewBackground.PNG (note the capitals)? You need to have the case match exactly on an iOS device, whereas it doesn't need to match exactly in the Simulator.

伪装你 2024-11-10 15:57:49
 UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableViewBackground.png"]];
[tempImageView setFrame:self.tableView.frame]; 

self.tableView.backgroundView = tempImageView;
 [tempImageView release];

这很完美。感谢乔纳的拉文

 UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableViewBackground.png"]];
[tempImageView setFrame:self.tableView.frame]; 

self.tableView.backgroundView = tempImageView;
 [tempImageView release];

This works perfect. Thanks to Ravin by Jona

天赋异禀 2024-11-10 15:57:49

一个 IBDesignable 表格视图子类,可让您从界面生成器附加图像

import UIKit

@IBDesignable
class DesignableTableView: UITableView {

    @IBInspectable var backgroundImage: UIImage? {
        didSet {
            if let image = backgroundImage {
                let backgroundImage = UIImageView(image: image)
                backgroundImage.contentMode = UIViewContentMode.ScaleToFill 
                backgroundImage.clipsToBounds = false
                self.backgroundView = backgroundImage
            }
        }
    }

}

An IBDesignable tableview subclass that will let you attach an image from interface builder

import UIKit

@IBDesignable
class DesignableTableView: UITableView {

    @IBInspectable var backgroundImage: UIImage? {
        didSet {
            if let image = backgroundImage {
                let backgroundImage = UIImageView(image: image)
                backgroundImage.contentMode = UIViewContentMode.ScaleToFill 
                backgroundImage.clipsToBounds = false
                self.backgroundView = backgroundImage
            }
        }
    }

}
梦太阳 2024-11-10 15:57:49
[TableView setBackgroundView:nil];
[TableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"apple.png"]] ];
[TableView setBackgroundView:nil];
[TableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"apple.png"]] ];
纵情客 2024-11-10 15:57:49

如果您想使用图案图像作为背景,这也非常有效。

UIImage *image = [UIImage imageNamed:@"something.png"];
self.tableView.backgroundView = nil;
self.view.backgroundColor = [UIColor colorWithPatternImage:image];

This works perfectly fine, also if you want to use a pattern image for your background.

UIImage *image = [UIImage imageNamed:@"something.png"];
self.tableView.backgroundView = nil;
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
一身软味 2024-11-10 15:57:49

有点晚了,但也许对于所有其他正在寻找解决方案的人来说。我可以通过在 UITableViewControllerviewDidLoad 方法中使用以下代码来完成这项工作:

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
self.tableView.backgroundColor = [UIColor clearColor];

或者仅使用背景颜色:

self.parentViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
self.tableView.backgroundColor = [UIColor clearColor];

我花了一段时间才弄清楚这一点,但是现在它就像一个魅力。

A little late but maybe for all the others looking for a solution. I could get that work by using the following code in the viewDidLoad method of the UITableViewController:

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
self.tableView.backgroundColor = [UIColor clearColor];

or with using just a background color:

self.parentViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
self.tableView.backgroundColor = [UIColor clearColor];

It took me a while to figure that out but now it works like a charm.

两个我 2024-11-10 15:57:49

Objective-c 版本:

cell.backgroundColor = [UIColor clearColor];

另请参阅:
​​如何创建具有透明背景的 UITableViewCell

Objective-c version:

cell.backgroundColor = [UIColor clearColor];

Also see this:
How to create a UITableViewCell with a transparent background

冰雪梦之恋 2024-11-10 15:57:49

感谢@Ravin 的回答。

在斯威夫特 5.0 中:

    let tempImageView = UIImageView(image: UIImage(named: "justin_bieber_topless.png"))
    tempImageView.frame = self.tableView.frame
    self.tableView.backgroundView = tempImageView;

Thanks @Ravin for your answer.

In Swift 5.0:

    let tempImageView = UIImageView(image: UIImage(named: "justin_bieber_topless.png"))
    tempImageView.frame = self.tableView.frame
    self.tableView.backgroundView = tempImageView;
扭转时空 2024-11-10 15:57:49
UIImageView *imageviewTemp = [[UIImageView alloc] init];
[(UIImageView *)imageviewTemp sd_setImageWithURL:[NSURL URLWithString:self.stringOfPassedImage]];
[imageviewTemp setFrame:self.tableView.frame];
self.tableView.backgroundView = imageviewTemp;
UIImageView *imageviewTemp = [[UIImageView alloc] init];
[(UIImageView *)imageviewTemp sd_setImageWithURL:[NSURL URLWithString:self.stringOfPassedImage]];
[imageviewTemp setFrame:self.tableView.frame];
self.tableView.backgroundView = imageviewTemp;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文