可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
How can I set an image for 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.
回答1:
sets the background
fill mode
[youTable setBackgroundView:
[[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"bg.png"]]];
sets the background
mode tile
[youTable setBackgroundColor:
[UIColor colorWithPatternImage:
[UIImage imageNamed:@"bg.png"]]];
回答2:
UIImage *image = [UIImage imageNamed:@"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UITableView *tableView = (UITableView*)tableViewController.view;
tableView.backgroundView = imageView;
[imageView release];
回答3:
This worked for me :
tableView.backgroundView =
[[UIImageView alloc]initWithImage:
[[UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0
topCapHeight:5.0]];
回答4:
If all the aforementioned suggestions don't work, just do it this way (the approach I always use):
- 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.
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.
回答5:
You can set the tableviews background view to a UIImageView instance (property backgroundView
of UITableView
).
回答6:
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];
回答7:
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"]]];
回答8:
i was also not able to set the backgroundColor
of UITableView
because it's a UITableViewController
, my issue solved by this
[self.tableView setBackgroundView:
[[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"bg.png"]]];
As suggested by the "WhiteTiger".
回答9:
Updating for 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:
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
}