【问题标题】:UITableViewController Background ImageUITableViewController 背景图片
【发布时间】:2012-04-21 10:09:42
【问题描述】:

如何为UITableViewController设置图片?

我用过很多东西,但它不能帮助我将图像设置为背景

UIImageView* bgView = [[[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"Default.png"]] autorelease];

[tableView.backgroundView addSubview:bgView];

还有这个

[[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];

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

这些都对我不起作用。

【问题讨论】:

    标签: iphone objective-c xcode xcode4 xcode4.2


    【解决方案1】:

    设置background 填充模式

    [youTable setBackgroundView:
      [[UIImageView alloc] initWithImage:
        [UIImage imageNamed:@"bg.png"]]];
    

    设置background 模式图块

    [youTable setBackgroundColor:
      [UIColor colorWithPatternImage:
        [UIImage imageNamed:@"bg.png"]]];
    

    【讨论】:

    • 我已经在我的项目中测试过了,很遗憾它不起作用
    • 我怀疑你给单元格设置了背景颜色,而这些覆盖了背景。
    • 我的单元格是默认的,我没有更改颜色,我必须将其更改为清除颜色还是默认为清除?
    • 记住它是 UITableViewController 而不是 tableview
    • 在viewDidLoad(例如)中尝试放:[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]]];然后 tableView: cellForRowAtIndexPath 尝试放置 cell.contentView.backgroundColor = [UIColor clearColor],所以你放置背景并使单元格透明,但请记住这取决于你放入的内容。你还需要使各种对象的背景透明,以 UILabel 等为例。..
    【解决方案2】:
    UIImage *image = [UIImage imageNamed:@"image.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    
    UITableView *tableView = (UITableView*)tableViewController.view;
    tableView.backgroundView = imageView;
    [imageView release];
    

    【讨论】:

      【解决方案3】:

      这对我有用:

      tableView.backgroundView = 
        [[UIImageView alloc]initWithImage:
          [[UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 
                                                  topCapHeight:5.0]];
      

      【讨论】:

      • 很好,但为了更好地理解用户,请解释一下。
      【解决方案4】:

      如果上述所有建议都不起作用,就这样做(我一直使用的方法):

      1. 创建一个 UIViewController 子类。
      2. 在 viewDidLoad 中添加 UIImageView 和 UITableView 作为子视图。
      3. 配置 tableView 并实现委托方法。在 imageView 中设置背景图片。

      我认为为 tableView 设置 backgroundView 可能会导致图形错误(为每个单元格重复图像),所以这就是我使用此处描述的方法的原因。

      【讨论】:

        【解决方案5】:

        您可以将 tableviews 背景视图设置为 UIImageView 实例(UITableView 的属性 backgroundView)。

        【讨论】:

          【解决方案6】:

          以下代码对我很有效。 你可以试试下面的代码:-

          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】:

            这对我有用:

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

            [self.tableView setBackgroundView:nil];
            [self.tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg2.png"]]];
            

            【讨论】:

              【解决方案8】:

              我也无法设置UITableViewbackgroundColor,因为它是UITableViewController,我的问题由此解决了

              [self.tableView setBackgroundView:
                [[UIImageView alloc] initWithImage:
                  [UIImage imageNamed:@"bg.png"]]];
              

              正如“白虎”所建议的那样。

              【讨论】:

              • 您正在为 iPad 开发? iPad setBackgroundColor 不起作用,你必须使用 setBackgroundView
              • 我没有在这里使用 setBackgroundColor,我正在为 iphone 工作
              【解决方案9】:

              为 Swift 4 更新

              我遇到的问题是我想向 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
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2013-08-16
                • 1970-01-01
                • 2019-03-24
                • 1970-01-01
                • 1970-01-01
                • 2018-10-11
                • 2013-12-05
                相关资源
                最近更新 更多