xcode UITableViewController 背景图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10066387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableViewController Background Image
提问by Youstanzr
How can I set an image for UITableViewController
?
如何设置图像UITableViewController
?
I have used many things but it doesn't help me to set the image as background
我使用了很多东西,但它不能帮助我将图像设置为背景
UIImageView* bgView = [[[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"Default.png"]] autorelease];
[tableView.backgroundView addSubview:bgView];
And this
和这个
[[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];
And
和
self.navigationController.view.backgroundColor =
[UIColor colorWithPatternImage:
[UIImage imageNamed:@"myImage.png"]];
self.tableView.backgroundColor = [UIColor clearColor];
None of it worked for me.
没有一个对我有用。
回答by WhiteTiger
sets the background
fill mode
设置background
填充模式
[youTable setBackgroundView:
[[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"bg.png"]]];
sets the background
mode tile
设置background
模式磁贴
[youTable setBackgroundColor:
[UIColor colorWithPatternImage:
[UIImage imageNamed:@"bg.png"]]];
回答by SVGreg
UIImage *image = [UIImage imageNamed:@"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UITableView *tableView = (UITableView*)tableViewController.view;
tableView.backgroundView = imageView;
[imageView release];
回答by user2771392
This worked for me :
这对我有用:
tableView.backgroundView =
[[UIImageView alloc]initWithImage:
[[UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0
topCapHeight:5.0]];
回答by Wolfgang Schreurs
If all the aforementioned suggestions don't work, just do it this way (the approach I alwaysuse):
如果上述所有建议都不起作用,就这样做(我一直使用的方法):
- Create a UIViewController subclass.
- In viewDidLoad add both an UIImageView and an UITableView as subviews.
- Configure the tableView and implement the delegate methods. Set the background image in the imageView.
- 创建一个 UIViewController 子类。
- 在 viewDidLoad 中添加一个 UIImageView 和一个 UITableView 作为子视图。
- 配置 tableView 并实现委托方法。在 imageView 中设置背景图片。
I think setting the backgroundView for a tableView can cause graphical errors (repeating images for each cell), so that's why I use the approach described here.
我认为为 tableView 设置 backgroundView 会导致图形错误(每个单元格重复图像),所以这就是我使用这里描述的方法的原因。
回答by Sunil Zalavadiya
Following code is works well for me. Can you try following code:-
以下代码对我来说效果很好。你可以试试下面的代码:-
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];
回答by SAHIL
i was also not able to set the backgroundColor
of UITableView
because it's a UITableViewController
, my issue solved by this
我也无法设置backgroundColor
,UITableView
因为它是一个UITableViewController
,我的问题由此解决了
[self.tableView setBackgroundView:
[[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"bg.png"]]];
As suggested by the "WhiteTiger".
正如“WhiteTiger”所建议的那样。
回答by emotality
This worked for me :
这对我有用:
self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg2.png"]];
or
或者
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg2.png"]]];
回答by Will Buffington
Updating for Swift 4
更新 Swift 4
The problem I was having was that I wanted to add a button and a background image to the UITableViewController. I added the button successfully, but every time I added the image container, it would replace the button instead of adding it below the button. So to get both, I added the following in the ViewWillAppear function:
我遇到的问题是我想向 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
}
回答by Felix
You can set the tableviews background view to a UIImageView instance (property backgroundView
of UITableView
).
您可以将 tableviews 背景视图设置为 UIImageView 实例(属性backgroundView
为UITableView
)。