9

如何设置图像UITableViewController

我已经使用了很多东西,但它并没有帮助我将图像设置为背景

UIImageView* bgView = [[[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"Default.png"]] autorelease];

[tableView.backgroundView addSubview:bgView];

还有这个

[[self view] setAutoresizesSubviews:NO];

UIView *backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
backgroundView.backgroundColor = 
  [UIColor colorWithPatternImage:
    [UIImage imageNamed:@"Default.png"]];

self.tableView.backgroundView = backgroundView;
self.tableView.backgroundColor = [UIColor clearColor];

[backgroundView release];

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

没有一个对我有用。

4

9 回答 9

25

设置background填充模式

[youTable setBackgroundView:
  [[UIImageView alloc] initWithImage:
    [UIImage imageNamed:@"bg.png"]]];

设置background模式图块

[youTable setBackgroundColor:
  [UIColor colorWithPatternImage:
    [UIImage imageNamed:@"bg.png"]]];
于 2012-04-08T20:58:02.513 回答
4
UIImage *image = [UIImage imageNamed:@"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

UITableView *tableView = (UITableView*)tableViewController.view;
tableView.backgroundView = imageView;
[imageView release];
于 2012-04-08T21:00:11.287 回答
3

这对我有用:

tableView.backgroundView = 
  [[UIImageView alloc]initWithImage:
    [[UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 
                                            topCapHeight:5.0]];
于 2013-09-12T06:08:23.430 回答
1

如果上述所有建议都不起作用,就这样做(我一直使用的方法):

  1. 创建一个 UIViewController 子类。
  2. 在 viewDidLoad 添加 UIImageView 和 UITableView 作为子视图。
  3. 配置 tableView 并实现委托方法。在 imageView 中设置背景图片。

我认为为 tableView 设置 backgroundView 可能会导致图形错误(为每个单元格重复图像),所以这就是我使用此处描述的方法的原因。

于 2012-04-08T21:05:36.943 回答
0

您可以将 tableviews 背景视图设置为 UIImageView 实例(属性backgroundViewUITableView

于 2012-04-08T21:00:28.443 回答
0

以下代码对我来说效果很好。您可以尝试以下代码:-

UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]];
bgView.frame = self.view.frame;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.backgroundView = bgView;
[bgView release];
于 2012-04-09T04:27:06.067 回答
0

我也无法设置backgroundColorofUITableView因为它是一个UITableViewController,我的问题由此解决

[self.tableView setBackgroundView:
  [[UIImageView alloc] initWithImage:
    [UIImage imageNamed:@"bg.png"]]];

正如“白虎”所建议的那样。

于 2013-01-15T07:52:29.637 回答
0

这对我有用:

self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg2.png"]];

或者

[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg2.png"]]];
于 2013-07-04T15:44:14.063 回答
0

更新 Swift 4

我遇到的问题是我想向 UITableViewController 添加一个按钮和一个背景图像。我成功添加了按钮,但是每次添加图像容器时,它都会替换按钮而不是在按钮下方添加它。因此,为了获得两者,我在 ViewWillAppear 函数中添加了以下内容:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Add a background view to the table view
    let backgroundImage = UIImage(named: "carbonfiber1.png")
    let imageView = UIImageView(image: backgroundImage)
    self.tableView.backgroundView = imageView
}
于 2018-09-18T15:31:07.807 回答