参考: https://stackoverflow.com/questions/813068/uitableview-change-section-header-color/813103

1.背景色

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {let header = view as! UITableViewHeaderFooterViewheader.backgroundView?.backgroundColor = .whiteheader.textLabel?.textColor = .blackheader.textLabel?.font = UIFont(name: "Helvetica-Bold", size: 14)
} 

2. title

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {return sections[section]}

3.遮挡了分隔线

cell的分隔线:

当设置了

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {return 1}

这样section就会有一条宽屏高度为1的分隔线,这样会遮挡了cell本来自定义的分隔线问题

4.section自定义样式

根据我之前写的https://blog.csdn.net/jameskaron/article/details/104358241

自定义背景色(willDisplayHeaderView)和自定义分隔线(viewForHeaderInSection) uiview有冲突

所以不能使用return headerView这个办法

新方法是自定义一个section类,参考:https://developer.apple.com/documentation/uikit/views_and_controls/table_views/adding_headers_and_footers_to_table_sections

这个方法十分好用,想怎么定义都可以

但是有些地方需要注意.

先注册:

portTable.register(MyCustomSectionHeader.self, forHeaderFooterViewReuseIdentifier: "sectionHeader")
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {let view = tableView.dequeueReusableHeaderFooterView(withIdentifier:"sectionHeader") as! MyCustomSectionHeaderview.title.text = sections[section]return view}

*** 在调用这里新建一个自定义view再view.addsubview()没有效果,想要的样式要全部做在自定义section类里面

这是我的自定义section类

import UIKitclass MyCustomSectionHeader: UITableViewHeaderFooterView {let title = UILabel()let seperatorLine = UIView()override init(reuseIdentifier: String?) {super.init(reuseIdentifier: reuseIdentifier)configureContents()}required init?(coder: NSCoder) {fatalError("init(coder:) has not been implemented")}func configureContents() {title.translatesAutoresizingMaskIntoConstraints = falseseperatorLine.translatesAutoresizingMaskIntoConstraints = falsetitle.textColor = .whitetitle.font = UIFont.systemFont(ofSize: 13)contentView.addSubview(title)contentView.addSubview(seperatorLine)seperatorLine.backgroundColor = UIStyleUtil.getInstance()?.getTableLineGrayColor()// Center the image vertically and place it near the leading// edge of the view. Constrain its width and height to 50 points.NSLayoutConstraint.activate([// Center the label vertically, and use it to fill the remaining// space in the header view.title.heightAnchor.constraint(equalTo: contentView.heightAnchor),title.leadingAnchor.constraint(equalTo: contentView.leadingAnchor,constant: 15),title.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),seperatorLine.heightAnchor.constraint(equalToConstant: 0.5),seperatorLine.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 15),seperatorLine.trailingAnchor.constraint(equalTo:contentView.trailingAnchor, constant: -15),seperatorLine.centerYAnchor.constraint(equalTo: contentView.bottomAnchor)])}}

*** 大体上和Apple的sample是一样的.我加了分隔线,分隔线要显示出来必须要要通过约束的设置,我试过通过类似以下的代码:

let separatorFooterView = UIView(frame: CGRect(x: tableView.separatorInset.left,y: headerView.frame.height + 20,width: tableView.frame.width - tableView.separatorInset.right - tableView.separatorInset.left,height: 1))

在类里面创建实例,但是没有用,只能通过约束

而且因为设置了label,所以上面willDisplayHeaderView的有修改:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {let header = view as! UITableViewHeaderFooterViewheader.backgroundView?.backgroundColor = .clear
//        header.textLabel?.textColor = .white
//        header.textLabel?.font = UIFont.systemFont(ofSize: 13)}

然后titleForHeaderInSection可以不用了

这种方式十分好用,同理我也可以设置footer,写一下willDisplayFooterView和viewForFooterInSection即可

[IOS]如何设置section的title和改变section的背景色及样式相关推荐

  1. ios wkweb设置图片_iOS WKWebView的使用

    WKWebView的使用 前言 最近项目中的UIWebView被替换为了WKWebView,因此来总结一下. 示例Demo:WKWebView的使用 本文将从以下几方面介绍WKWebView: 1.W ...

  2. iOS状态栏设置详解

    在iOS开发过程中,经常会设置状态栏的样式,所以这里来学习一下状态栏的常用的设置方法. 1.首先明白状态栏的基本概念 iOS上状态栏 就是指的最上面的20像素高的部分 状态栏分前后两部分,要分清这两个 ...

  3. android button 中文字位置设置,iOS开发-- 设置UIButton的文字显示位置、字体的大小、字体的颜色...

    btn.frame = CGRectMake(x, y, width, height); [btn setTitle: @"search" forState: UIControlS ...

  4. Vue 中设置浏览器的 title 跟随路由的名称变化

    Vue 中设置浏览器的 title 跟随路由的名称变化 浏览器title的变化会根据路由的变化而改变 可以先设置一个公共的文件夹setting, 用于存放公共的名称, 例如这里的 `小火车况且况且 m ...

  5. IOS开发设置应用程序App启动图片

    IOS开发设置应用程序App启动图片 在资源Assets 的图片区域 右键建立一个Launch Image ,如第二张图,然后把相应的启动图片拖进去. 最后选中项目,找到 Assets ,设置 启动图 ...

  6. iOS 怎么设置 UITabBarController 的第n个item为第一响应者?

    iOS 怎么设置 UITabBarController 的第n个item为第一响应者? UITabBarController 里面有个属性:selectedIndex @property(nonato ...

  7. 李洪强iOS之集成极光推送二iOS 证书 设置指南

    李洪强iOS之集成极光推送二iOS 证书 设置指南 创建应用程序ID 登陆 iOS Dev Center 选择进入iOS Provisioning Portal. 在 iOS Provisioning ...

  8. 设置超链接在各种状态改变的样式颜色

    设置超链接在各种状态改变的样式颜色,在html的<head>标签下面添加下面的样式,可以自己根据需要修改样式. <style> a:link {color:blue;} a:v ...

  9. angular5+动态设置页面标题title

    在实际的开发项目当中,经常需要对访问网页的title进行动态配置,就像这样的东东, 所以今天来记录下怎么动态配置title 一.首先,准备一个  .json的配置文件放置在静态资源assets文件夹下 ...

最新文章

  1. 国办发明电〔2020〕27号:国务院办公厅关于2021年部分节假日安排的通知
  2. Deep Learning论文笔记之(七)深度网络高层特征可视化
  3. hadoop 启动提示输入password的问题
  4. Mybatis Plus——AutoGenerator配置BaseResultMap(通用查询映射结果)和BaseColumnList(通用查询结果列)自动生成
  5. js Object的属性 Configurable,Enumerable,Writable,Value,Getter,Setter
  6. IOS:批量导入图片和视频到模拟器的相册
  7. Java加密与解密的艺术~数字签名~ECDSA实现
  8. mysql查询时,offset过大影响性能的原因与优化方法
  9. Hibernate框架 一对多,多对多映射
  10. 服务器部署ssl证书,Apache服务器SSL证书部署
  11. strcpy(d,s)的溢出问题
  12. 安装WindowBuilder后,新建JFrame文件,不能查看Design
  13. UML类图中箭头和线条的含义和用法
  14. 3 EDA技术实用教程 【基础知识1】
  15. 阿里云服务器怎么正确使用OSS内网地址?
  16. 一闪一闪的星星特效效果
  17. 3DMAx Panda Directx Exporter 导出 X插件
  18. ISP芯片:如何让数字之眼“看清”真实世界?
  19. 计算机鼠标不动了,鼠标不动了怎么办,教您鼠标不动了怎么办
  20. 你是如何界定高低模?听听大佬怎么说!零基础小白须知!

热门文章

  1. 移动端开发需要注意的事项
  2. Corona建筑渲染作品: Rukomo卢旺达教堂
  3. 直角坐标XY和经纬度BL正算反算(西安80坐标3度分带)php
  4. Qt——管道FIFO的创建与使用【未完成】
  5. 零基础转行如何快速成为一名高薪的Python开发程序员
  6. java怎么关掉定时器_java – 取消定时器
  7. Windows下的OpenCVSharp配置
  8. 深度学习实战(一)快速理解实现风格迁移
  9. 【转】C# 实现用艺术效果显示文本的标签控件
  10. 西门子PLC基于MODBUS RTU通信协议时的寄存器地址对应关系及错误代码