在基于导航的应用程序中将背景图像添加到UITableViewController
-
06-07-2019 - |
题
我有一个基于导航的应用程序,我将 UITableViewControllers
推入堆栈。我想在我的所有 UITableViewControllers
中添加一个背景 UImage
。不是 UIColor
,而是 UImage
。我知道如何使用 Nib
文件并设置 UITableView
本身来使用 [UIColor ClearColor]
,但我不知道如何做到这一点想要浏览我的所有 UITableViewControllers
并将它们更改为使用 Nib
文件等。
我还发现这个解决方案,如果我在我的应用程序中只使用一个tableviewcontroller,这将是很棒的。我认为可能有一种方法可以通过添加子视图“下面”来实现这项工作。我的表视图是默认在 UITableViewController
中创建的?
任何建议都会很棒。
解决方案
基于导航的应用程序略有不同:只需更改每个表视图所在的导航视图的背景。将以下代码放在每个UITableViewController的 viewDidLoad
中:
self.navigationController.view.backgroundColor =
[UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png"]];
self.tableView.backgroundColor = [UIColor clearColor];
但您可能只需要在导航控制器的顶层执行一次,而不是在每个tableview控制器中执行此操作(尽管您仍需要将每个背景设置为清除)。
其他提示
在iOS6上使用:
UIImageView *boxBackView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TextureBoxboard.jpg"]];
[self.tableView setBackgroundView:boxBackView];
如果您的类是UIViewController子类,那么您可以这样做:
[self.view setBackgroundColor:
[UIColor colorWithPatternImage:
[UIImage imageWithContentsOfFile:
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
@"background.png"]]]];
UIImageView *backgroundView =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
backgroundView.frame = CGRectMake(0,
0,
self.navigationController.view.frame.size.width,
self.navigationController.view.frame.size.height);
backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
[self.navigationController.view insertSubview:backgroundView atIndex:0];
[backgroundView release];
self.tableView.backgroundColor = [UIColor clearColor];
正如Gorm所说
只需要在
的顶层执行一次UINavigationController
给我Madhup的答案是正确的答案。 UITableViewController是UIViewController的子类,因此将它添加到UITableViewController的viewDidLoad方法效果很好。
从iOS 3.2开始, - [UITableView setBackgroundView:]存在,这可能比其他一些提出的解决方案更容易。