【问题标题】:The background colour of a grouped UITableView will not go transparent in iOS7分组 UITableView 的背景颜色在 iOS7 中不会透明
【发布时间】:2013-09-22 13:17:19
【问题描述】:

我正在开发一个应用程序,它在 iOS 6 上运行得非常好。在 iOS7 上,该应用程序有几个布局和一般的外观问题。一个是无法将分组表视图的背景颜色设置为透明。这是我的代码,它不再起作用了

    tableForDetails = [[UITableView alloc]initWithFrame:CGRectMake(0, yAxisTable, 320, 150) style:UITableViewStyleGrouped];
    UIColor *backGroundColor = [UIColor clearColor];
    UIView *bview = [[UIView alloc]init];
    [bview setBackgroundColor:backGroundColor];
    [tableForDetails setBackgroundView:bview];

非常感谢您的帮助。谢谢

【问题讨论】:

    标签: uitableview transparency ios7 xcode5


    【解决方案1】:

    在 iOS7 中为分组的 uiTableview 设置背景透明度真的很容易。解决方案比我最初想象的还要简单。

    [tableForDetails setBackgroundColor:[UIColor clearColor]];
    

    或仅用于半透明

    [tableForDetails setBackgroundColor:[[UIColor alloc]initWithRed:1.0 green:1.0 blue:1.0 alpha:0.5]];
    

    这对我来说很好。

    【讨论】:

    • 恕我直言,尺寸不是关键要求。这也可以 UIView* bgv = [[UIView alloc] init];bgv.backgroundColor = [UIColor clearColor]; [self.tableView setBackgroundColor:[UIColor clearColor]]; [self.tableView setBackgroundView:bgv];关键似乎是 setBackgroundColor
    • 感谢您的评论。我意识到你是对的。我将编辑答案。
    【解决方案2】:

    我用这个修复了它:

    tableView.backgroundView = UIView.new;
    

    【讨论】:

      【解决方案3】:

      我将 Apple 组件的任何改进都放在了一个超类中。 BasicTableView.m 中应用了以下升级,扩展了UITableView

      1) 覆盖 setBackgroundColor 以与 iOS 7 及更高版本一起使用,同时保持与 iOS 6 和 5 的向后兼容性。
      2) 覆盖initWithCoder:initWithFrame:style: 将背景颜色初始化为默认透明背景

      - (id) initWithCoder:(NSCoder *)decoder
      {
          self = [super initWithCoder:decoder];
          if (self) {
              [self setup];
          }
      
          return self;
      }
      
      - (id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style
      {
          self = [super initWithFrame:frame style:style];
      
          if (self) {
               [self setup];
          }
      
          return self;
      }
      
      - (void) setup
      {
          //Set the BG color to clear color by default
          [self setBackgroundColor:[UIColor clearColor]];
      }
      
      - (void) setBackgroundColor:(UIColor *)backgroundColor
      {
          if (self.style == UITableViewStyleGrouped) {
              if (majorSystemVersion() < 7) {
                  UIView *tableBgView = [[UIView alloc] init];
                  tableBgView.backgroundColor = backgroundColor;
                  self.backgroundView = tableBgView;
              } else {
                  [super setBackgroundColor:backgroundColor];
              }
      
          }
      }
      
      //majorSystemVersion() is defined elsewhere in a utility file
      int majorSystemVersion(void) {
          NSString *systemVersion = [UIDevice currentDevice].systemVersion;
          NSArray *systemVersionComps = [systemVersion componentsSeparatedByString:@"."];
          NSString *majorVersion = [systemVersionComps objectAtIndex:0];
      
          return [majorVersion intValue];
      }
      

      【讨论】:

        【解决方案4】:

        UITableViewCell 在 iOS 7 中默认为白色背景

        把它放在你的 cellforRowAtIndexPath 中

        [单元格设置背景颜色:[UIColor clearColor]]

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-25
          • 1970-01-01
          • 1970-01-01
          • 2011-08-30
          • 1970-01-01
          • 2012-11-05
          • 2012-06-26
          • 2022-08-17
          相关资源
          最近更新 更多