【问题标题】:How do I set a background image for a grouped table view?如何为分组表视图设置背景图像?
【发布时间】:2010-11-25 03:28:58
【问题描述】:

我见过一些 iPhone 应用程序使用自定义图像作为分组 UITableView 的背景,而不是标准的灰线。

这是如何实现的?

【问题讨论】:

    标签: iphone image uitableview background


    【解决方案1】:

    这就是对我有用的方法(一旦我弄清楚了就相当简单;)

    1) 在您的应用委托中添加一个视图,并使其成为窗口的子视图:

    UIView *bgView = [[UIView alloc]initWithFrame:window.frame];
    
     bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"screenBG.png"]];
     [window addSubview:bgView];
     [bgView release];
    

    2) 在每个视图控制器 .m 文件中,在 ViewDidLoad 下,将该特定视图的背景颜色设置为透明(这样上面创建的另一个 bgView 就会显示出来):

    self.view.backgroundColor = [UIColor clearColor];
    

    在我的例子中,第 2 步中的视图控制器是一个 tableviewcontroller。看起来很棒。

    顺便说一句,在每个视图控制器中执行以下操作效果不佳:

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"screenBG.png"]];
    

    所以按照上面的步骤 1 和 2。

    希望对您有所帮助, 骨干

    【讨论】:

    • 在应用委托中你也可以更轻松地做到:self.window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];
    • 我同意 Gil Margolin 的观点……将窗口的背景颜色设置为图像要容易得多。有一个警告 - 您必须手动加载视网膜/iphone5 的正确图像,这有点复杂。
    【解决方案2】:

    试试这个

    - (void) viewDidLoad {
        [super viewDidLoad];
    
        self.tableView.backgroundColor = [UIColor clearColor];
        self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"wallpaper.png"]];
        self.tableView.opaque = NO;
    }
    

    【讨论】:

    • 你也可以在table view后面添加一个image view,而不是设置背景色,这样可以避免使用pattern image。
    【解决方案3】:

    在另一个项目(使用 2.2.1 开发)中,我通过将 UITableView's 背景不透明度设置为 0%,然后使用 Interface Builder 在其后面简单地分层 UIImageView 来做到这一点。无论表格状态如何,这让我有一个固定的背景。您也可以将UITableView 的背景设置为图像,但随后背景会随表格滚动。 (目前我没有手头的代码,但不久前我在 Apple 开发者论坛上得到了提示)。

    请注意,这可能会导致一些性能问题。 Apple 不鼓励尽可能使用透明度,因为 3GS 之前的型号上的 GPU 并不是特别强大。

    【讨论】:

      【解决方案4】:

      您可以像这样使用+[UIColor colorWithPatternImage:(UIImage)] 方法:

      self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
      

      【讨论】:

        【解决方案5】:

        colorWithPatternImage: 的问题是你需要使用“有图案”的图像,否则你的图像会被随机平铺

        此链接有一个简单的解决方案,如果您希望所有视图具有相同的背景

        http://howtomakeiphoneapps.com/2009/03/how-to-add-a-nice-background-image-to-your-grouped-table-view/

        【讨论】:

          【解决方案6】:
          self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"SortByCategory_320x480.png"]];
          
          self.tableView.separatorColor = [UIColor clearColor];
          
          self.tableView.backgroundColor = [UIColor clearColor];
          

          希望这会有所帮助。它不会在单元格后面显示可怕的半透明背景,尤其是在 Grouped UITableView 的情况下。

          【讨论】:

          • 访问 parentViewController.view 是一个糟糕的想法——你不知道它有什么样的支持,而且这是对封装的严重违反。即使它现在可以工作,下一次 iOS 更新也可能完全破坏你的代码。不要这样做。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-11
          • 2013-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-25
          相关资源
          最近更新 更多