如何为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];
这些都不适合我。
设置background
填充模式
[youTable setBackgroundView:
[[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"bg.png"]]];
设置background
模式磁贴
[youTable setBackgroundColor:
[UIColor colorWithPatternImage:
[UIImage imageNamed:@"bg.png"]]];
UIImage *image = [UIImage imageNamed:@"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UITableView *tableView = (UITableView*)tableViewController.view;
tableView.backgroundView = imageView;
[imageView release];
这对我有用:
tableView.backgroundView =
[[UIImageView alloc]initWithImage:
[[UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0
topCapHeight:5.0]];
如果上述所有建议都不起作用,那就这样做(我总是使用的方法):
我认为为tableView设置backgroundView会导致图形错误(为每个单元重复图像),这就是我使用此处描述的方法的原因。
您可以将tableviews背景视图设置为UIImageView实例(backgroundView
的属性UITableView
)。
以下代码对我来说很有用。你可以尝试下面的代码: -
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];
这对我有用:
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"]]];
我也无法设置backgroundColor
的UITableView
,因为它是UITableViewController
,我的问题由此解决
[self.tableView setBackgroundView:
[[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"bg.png"]]];
正如“WhiteTiger”所暗示的那样。
更新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
}