Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在tableview xcode中调整标题大小和更改标题颜色_Ios_Swift - Fatal编程技术网

Ios 如何在tableview xcode中调整标题大小和更改标题颜色

Ios 如何在tableview xcode中调整标题大小和更改标题颜色,ios,swift,Ios,Swift,我正在故事板上使用静态表视图 这些章节都有标题 标题文本的颜色和大小是静态的,我无法更改它 它会导致非常狭窄的标题和黑色文本 我怎样才能腾出标题(使高度大一点)并改变文本的颜色 我怎样才能腾出标题(使高度大一点)并改变文本的颜色 您需要有一个带有标签的自定义视图,并将其返回到UITableView的viewForHeaderInSection的委托方法 func tableView(_ tableView: UITableView, viewForHeaderInSection sectio

我正在故事板上使用静态表视图 这些章节都有标题

标题文本的颜色和大小是静态的,我无法更改它 它会导致非常狭窄的标题和黑色文本

我怎样才能腾出标题(使高度大一点)并改变文本的颜色

我怎样才能腾出标题(使高度大一点)并改变文本的颜色

您需要有一个带有标签的自定义视图,并将其返回到
UITableView
viewForHeaderInSection
的委托方法

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
Ref:

  • 编辑:

    下面是如何做到这一点。基本上,在我上面提到的委托方法中需要一个自定义视图。如果您以前在
    cellForRow
    中成功地创建了自定义
    UITableViewCell
    ,那么这一个对您来说应该是小菜一碟

    声明一个容器视图,然后在该容器中添加子视图,在本例中,添加一个
    UILabel
    。我总是使用约束,例如:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        // Let's make the even numbers sections a red background.
        // Blue background for odd numbers
    
        let container = UIView()
        container.backgroundColor = section % 2 == 0 ? .red : .blue
    
        let titleForHeaderLabel = UILabel()
        titleForHeaderLabel.backgroundColor = .white
        titleForHeaderLabel.text = "HEADER SECTION: \(section)"
        container.addSubview(titleForHeaderLabel)
    
        titleForHeaderLabel.translatesAutoresizingMaskIntoConstraints = false
    
        titleForHeaderLabel.topAnchor.constraint(equalTo: container.topAnchor, constant: 20).isActive = true
        titleForHeaderLabel.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: -20.0).isActive = true
        titleForHeaderLabel.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 20.0).isActive = true
        titleForHeaderLabel.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -20.0).isActive = true
    
        return container
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 80.0
    }
    
    然后在
    func tableView(tableView:UITableView,heightForHeaderInSection:Int)->CGFloat
    delegate方法中为节提供高度,如下所示:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        // Let's make the even numbers sections a red background.
        // Blue background for odd numbers
    
        let container = UIView()
        container.backgroundColor = section % 2 == 0 ? .red : .blue
    
        let titleForHeaderLabel = UILabel()
        titleForHeaderLabel.backgroundColor = .white
        titleForHeaderLabel.text = "HEADER SECTION: \(section)"
        container.addSubview(titleForHeaderLabel)
    
        titleForHeaderLabel.translatesAutoresizingMaskIntoConstraints = false
    
        titleForHeaderLabel.topAnchor.constraint(equalTo: container.topAnchor, constant: 20).isActive = true
        titleForHeaderLabel.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: -20.0).isActive = true
        titleForHeaderLabel.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 20.0).isActive = true
        titleForHeaderLabel.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -20.0).isActive = true
    
        return container
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 80.0
    }
    
    输出:


    简单,对吗?:)我希望这有帮助

    好吧,让我试试,既然你已经向赏金提出了这个问题;):P