【问题标题】:add background image to UITableViewController in Navigation based app在基于导航的应用程序中将背景图像添加到 UITableViewController
【发布时间】:2026-02-04 15:30:01
【问题描述】:

我有一个基于导航的应用程序,我将UITableViewControllers 推送到堆栈上。我想为我所有的UITableViewControllers 添加背景UImage。不是UIColor,而是UImage。我知道如何使用Nib 文件并将UITableView 本身设置为使用[UIColor ClearColor],但我不想浏览所有UITableViewControllers 并将它们更改为使用Nib 文件等。

我还发现了this solution,如果我只是在我的应用程序中使用单个 tableviewcontroller,那就太好了。我认为可能有一种方法可以使这项工作,通过在我的表格视图“下方”添加一个子视图,该子视图默认在 UITableViewController? 中创建?

任何建议都会很棒。

【问题讨论】:

    标签: iphone uinavigationcontroller uiimage uitableview


    【解决方案1】:

    在基于导航的应用程序中有点不同:只需更改每个表格视图所在的导航视图的背景。将以下代码放在每个 UITableViewController 的viewDidLoad 中即可:

    self.navigationController.view.backgroundColor = 
    [UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png"]];
    self.tableView.backgroundColor = [UIColor clearColor];
    

    但您可能只需要在导航控制器的顶层执行一次,而不是在每个 tableview 控制器中执行一次(尽管您仍然必须将每个背景设置为清除)。

    【讨论】:

    • 另外我需要第三行:self.view.backgroundColor = [UIColor clearColor];
    • 这不会像视图控制器那样将背景图像推到导航栏的底部...
    • 我在 tableVC 中使用这个:[self.tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]]];像魅力一样工作;)
    • 这里有新问题:*.com/questions/12520585/…
    • @SteveMoser 我在这个问题中添加了 iOS 解决方案
    【解决方案2】:

    在 iOS6 上使用这个:

    UIImageView *boxBackView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TextureBoxboard.jpg"]];
    [self.tableView setBackgroundView:boxBackView];
    

    【讨论】:

    • 为什么选择iOS6? @property(nonatomic, readwrite, retain) UIView *backgroundView NS_AVAILABLE_IOS(3_2); // the background view will be automatically resized to track the size of the table view. this will be placed as a subview of the table view behind all cells and headers/footers. default may be non-nil for some devices.
    • 是的,我知道。但是这里提到的另一个解决方案不适用于 iOS6,而且我认为这个解决方案在
    【解决方案3】:

    如果你的类是 UIViewController 子类,那么你可以这样做:

    [self.view setBackgroundColor:
         [UIColor colorWithPatternImage:
          [UIImage imageWithContentsOfFile:
           [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
            @"background.png"]]]];
    

    【讨论】:

    • 谢谢,但这实际上是我问题的症结所在。它不是 UIViewController - 它是 UITableViewController。
    • 你指的是什么教程?
    【解决方案4】:
    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];
    

    正如戈姆所说的

    只需要做一次,在UINavigationController的顶层

    【讨论】:

      【解决方案5】:

      我的 Madhup 给出的答案是正确答案。 UITableViewController 是 UIViewController 的子类,因此将其添加到 UITableViewController 的 viewDidLoad 方法中效果很好。

      【讨论】:

        【解决方案6】:

        从 iOS 3.2 开始,-[UITableView setBackgroundView:] 存在,这可能比其他一些未来提出的解决方案更容易。

        【讨论】:

          最近更新 更多