UITableViewController背景图片

问题描述 投票:9回答: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];

这些都不适合我。

iphone objective-c xcode xcode4 xcode4.2
9个回答
25
投票

设置background填充模式

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

设置background模式磁贴

[youTable setBackgroundColor:
  [UIColor colorWithPatternImage:
    [UIImage imageNamed:@"bg.png"]]];

4
投票
UIImage *image = [UIImage imageNamed:@"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

UITableView *tableView = (UITableView*)tableViewController.view;
tableView.backgroundView = imageView;
[imageView release];

3
投票

这对我有用:

tableView.backgroundView = 
  [[UIImageView alloc]initWithImage:
    [[UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 
                                            topCapHeight:5.0]];

1
投票

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

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

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


0
投票

您可以将tableviews背景视图设置为UIImageView实例(backgroundView的属性UITableView)。


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

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"]]];

0
投票

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

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

正如“WhiteTiger”所暗示的那样。


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
}
© www.soinside.com 2019 - 2024. All rights reserved.