Как я могу установить градиент заголовка раздела UITableView (и прозрачный) фон в XCode? - PullRequest
0 голосов
/ 13 февраля 2020

Я пробовал это несколькими способами, но, к сожалению, не могу. Лучшим на данный момент был комментарий .

Я думаю, что решение должно быть в функции func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int), но я понятия не имею, как это сделать.

Я бы хотел получить что-то вроде этого . Я использую Swift 5+, и цель iOS может быть чем угодно (13+).

1 Ответ

0 голосов
/ 13 февраля 2020

1. Используйте ViewForHeaderInSection, как показано ниже:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = self.getGradientBackgroundView()

    // Add any title to the Header if needed
    let label = UILabel()
    label.frame = CGRect.init(x: 5, y: 5, width: headerView.frame.width-10, height: headerView.frame.height-10)
    label.text = "My header"
    headerView.addSubview(label)

    return headerView
}

2. Установите Высота заголовка, используя heightForHeaderInSection:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50 //whatever height you needed.
    }

3. Чтобы получить BG Header View

  private func getGradientBackgroundView() -> UIView {
    let gradientBackgroundView = UIView()

    // Prepare Gradient Layer
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame.size = CGSize(width: self.myTableView.frame.size.width, height: 50) // height same as in heightForHeaderInSection
    gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
    gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
    gradientLayer.colors = [UIColor.blue.cgColor, UIColor.green.cgColor]
    // Add layer to view
    gradientBackgroundView.layer.addSublayer(gradientLayer)
    return gradientBackgroundView
  }

Надеюсь, это поможет !!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...