UILabel 不会固定到 NavBar 的底部

UILabel Won't Pin To Bottom of NavBar

有没有办法让标签固定在底部而不是顶部?

    var timeLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = .clear
        label.textColor = .gray
        label.font = UIFont.systemFont(ofSize: 15, weight: .bold)
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    func setTitle(subtitle:UILabel) -> UIView {
        
        let navigationBarHeight = Int(self.navigationController!.navigationBar.frame.height)
        let navigationBarWidth = Int(self.navigationController!.navigationBar.frame.width)

        let titleView = UIView(frame: CGRect(x: 0, y: 0, width: navigationBarWidth, height: navigationBarHeight))
        titleView.addSubview(subtitle)

        return titleView

    }

然后在 viewDidLoad

self.navigationItem.titleView = setTitle(subtitle: timeLabel)

更新

将标签设置为右对齐:

var timeLabel: UILabel = {
    let label = UILabel()
    label.backgroundColor = .clear
    label.textColor = .gray
    label.textAlignment = .right
    label.font = UIFont.systemFont(ofSize: 15, weight: .bold)
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
}()

现在在 viewDidLoad 中将标签添加到导航栏并设置约束:

if let navigationBar = self.navigationController?.navigationBar {
   timeLabel.text = "FRIDAY, SEPTEMBER 18" // set here your string date
   navigationBar.addSubview(timeLabel)
   timeLabel.leadingAnchor.constraint(equalTo: navigationBar.leadingAnchor, constant: 150).isActive = true // set the constant how do you prefer
   timeLabel.trailingAnchor.constraint(equalTo: navigationBar.trailingAnchor, constant: -20).isActive = true
   timeLabel.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor, constant: 0).isActive = true
   timeLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true
 }

TABLE 视图滚动更新

将 UIScrollViewDelegate 添加到您的控制器,然后像这样实现 scrollViewWillBeginDragging 和 tableView willDisplay 方法:

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    if scrollView.contentSize.height > 0 && ((scrollView.contentOffset.y + scrollView.safeAreaInsets.top) == 0) {
        timeLabel.alpha = 0
    }
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if tableView.contentOffset.y <= 0 {
        timeLabel.alpha = 1
    }
}

奖金

为了在 timeLabel 再次显示时获得炫酷效果,在 tableView willDisplay 中添加动画,如下所示:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if tableView.contentOffset.y <= 0 {
        UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut) {
            self.timeLabel.alpha = 1
        } completion: { (_) in
            print("visible TimeLabel")
        }
    }
}

这是结果

吉尔更新

上面解释的不是规则,只是一个技巧,我建议你考虑把日期放在header 这样你就不会再有标题问题了:删除 scrollViewWillBeginDragging 和 tableView 的 willDisplay 函数和添加 viewForHeaderInSection,为此删除现有的 timeLabel 约束并声明 viewForHeaderInSection:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let v = UIView()
    v.backgroundColor = UIColor(red: 0.9018846154, green: 0.9020112157, blue: 0.9018446803, alpha: 1)
    v.addSubview(timeLabel)
    // declare here your timeLabel constraints
    timeLabel.topAnchor.constraint(equalTo: v.topAnchor).isActive = true
    timeLabel.trailingAnchor.constraint(equalTo: v.trailingAnchor).isActive = true
    timeLabel.leadingAnchor.constraint(equalTo: v.leadingAnchor).isActive = true
    timeLabel.bottomAnchor.constraint(equalTo: v.bottomAnchor).isActive = true
    return v
}

现在设置您 header 的高度:

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

这是结果: