【问题标题】:What is the default TableView section header background color on the iPhone?iPhone 上默认的 TableView 部分标题背景颜色是什么?
【发布时间】:2012-02-03 06:38:42
【问题描述】:

我想自定义 TableView 部分标题并保留默认背景颜色。我使用-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section。我需要根据设备(iPad 或 iPhone)更改字体大小。为此调用下一个函数:

[UIColor colorWithHue:0.6 饱和度:0.33 亮度:0.69 alpha:0.6]。

但我手动找到了这些值。

【问题讨论】:

    标签: uitableview tableview


    【解决方案1】:

    十六进制颜色 - #e8e9ed

    RGB 颜色 - UIColor(红色:232/255,绿色:233/255,蓝色:237/255,alpha:1)

    或者你可以使用#f7f7f7

    【讨论】:

      【解决方案2】:

      对于 iOS 11,我在模拟器上选择了它的颜色。

      • 对象 c:[UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1.0]

      • swift : UIColor(red: 247/255.0, green: 247/255.0, blue: 247/255.0, alpha: 1.0)

      但是,每当 Apple 更改颜色时,我们都需要更改。

      如何获取默认的节标题颜色?

      【讨论】:

        【解决方案3】:

        目标-C:

        [UIColor colorWithRed:232/255.0f green:233/255.0f blue:237/255.0f alpha:1.0f]
        

        斯威夫特:

        UIColor(red: 232/255, green: 233/255, blue: 237/255, alpha: 1)
        

        【讨论】:

        • 适用于 iOS 9 及更高版本
        • RGB 颜色:#e8e9ed
        【解决方案4】:

        用于更改 TableView 标题部分的背景颜色只需一行内容 添加TableView委托willDisplayHeaderView

        - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
        
           UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
           **header.tintColor = [UIColor whiteColor];**
        
        }
        

        【讨论】:

          【解决方案5】:

          ios7默认的tableview section header背景颜色是

          Objective-C
          [UIColor colorWithRed:247/255.0f green:247/255.0f blue:247/255.0f alpha:1.0f]
          
          Swift
          UIColor(red: 247/255, green: 247/255, blue: 247/255, alpha: 1)
          

          【讨论】:

          • 由于所有颜色分量都相同 (247/255),因此更简单的替代方法是:UIColor(white: 0.97, alpha: 1) 其中 0.97 ~ 247/255
          • 我希望这个答案能针对 iOS13 和暗模式进行更新。有没有我们可以使用的标准 UIColor 来代替那些硬编码的值?
          【解决方案6】:

          我在我的一个应用程序中所做的是像这样创建文本:

          NSString *sectionTitle = @"YOUR TITLE HERE";
          CGSize sz = [sectionTitle  sizeWithFont:[UIFont boldSystemFontOfSize:20.0f] 
                             constrainedToSize:CGSizeMake(290.0f, 20000.0f)
                                 lineBreakMode:UILineBreakModeWordWrap];
          
          CGFloat height = MAX(sz.height, 20.0f);
          
          CGRect sectionFrame = CGRectMake(0.0, 0.0, 320.0, height);
          

          我使用 290 作为约束宽度,以在两侧提供标签边框。然后我使用了这样的可拉伸图像:

          并对其进行缩放以适合标题中的文本:

              UIImage *headerImage = [UIImage imageNamed:@"sectionheaderbackground.png"];
              UIImage *stretchableImage = [headerImage stretchableImageWithLeftCapWidth:12 topCapHeight:0];
              UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:sectionFrame];
              backgroundImageView.image = stretchableImage;
          
              // Add it to the view for the header
              UIView *sectionView = [[UIView alloc] initWithFrame:sectionFrame];
              sectionView.alpha = 0.9; 
              sectionView.backgroundColor = [UIColor clearColor];
              [sectionView addSubview:backgroundImageView];
          

          最后,我创建了一个标签并将其添加为子视图:

              CGRect labelFrame = CGRectMake(10.0, 0.0, 290.0, height);
              UILabel *sectionLabel = [[UILabel alloc] initWithFrame:labelFrame];
              sectionLabel.text = sectionTitle;
              sectionLabel.numberOfLines = 0;
              sectionLabel.font = [UIFont boldSystemFontOfSize:18.0];
              sectionLabel.textColor = [UIColor whiteColor];
              sectionLabel.shadowColor = [UIColor grayColor];
              sectionLabel.shadowOffset = CGSizeMake(0, 1);
              sectionLabel.backgroundColor = [UIColor clearColor];
              [sectionView addSubview:sectionLabel];
          
              return sectionView;
          

          【讨论】:

            猜你喜欢
            • 2011-12-24
            • 2011-06-07
            • 1970-01-01
            • 2013-11-09
            • 1970-01-01
            • 2023-01-22
            • 2023-03-30
            • 1970-01-01
            • 2011-10-09
            相关资源
            最近更新 更多