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

ios – 如何在tableview xcode中调整大小和更改标题的颜色,第1张

概述我在故事板上使用静态Tableview 这是各部分的头条新闻 标题文字颜色大小是一个静态的东西,我不能改变它 它导致非常狭窄的标题和黑色文本. 如何区分标题(使高度更大)并更改文本的颜色? how can i space out the headlines (make the height a bit bigger) and change the color of the text ? 您需要具 我在故事板上使用静态tablevIEw
这是各部分的头条新闻

标题文字颜色和大小是一个静态的东西,我不能改变它
它导致非常狭窄的标题和黑色文本.

如何区分标题(使高度更大)并更改文本的颜色?

解决方法

how can i space out the headlines (make the height a bit bigger) and change the color of the text ?

您需要具有标签的自定义视图,并将其返回到UItableVIEw的vIEwForheaderInSection的委托方法.

func tableVIEw(_ tableVIEw: UItableVIEw,vIEwForheaderInSection section: Int) -> UIVIEw?

参考:

> https://www.hackingwithswift.com/example-code/uikit/how-to-add-a-section-header-to-a-table-view
> https://developer.apple.com/documentation/uikit/uitableview/1614965-headerview

编辑:

以下是如何实现这一目标.基本上,您需要在我上面提到的委托方法中使用自定义视图.如果你以前在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委托方法,如下:

func tableVIEw(_ tableVIEw: UItableVIEw,heightForheaderInSection section: Int) -> CGfloat {    return 80.0}

输出:

容易,对吗? :) 我希望这有帮助!

总结

以上是内存溢出为你收集整理的ios – 如何在tableview xcode中调整大小和更改标题的颜色全部内容,希望文章能够帮你解决ios – 如何在tableview xcode中调整大小和更改标题的颜色所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1063683.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存