UILabel不会固定到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)

Image

更新

Image3

解决方法

将标签的文本对齐设置为右:

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
 }

更新表视图滚动

将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中添加动画的效果很酷:

func tableView(_ tableView: UITableView,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")
        }
    }
}

这是结果

enter image description here

更新吉尔

以上解释的不是规则,而只是一个技巧,我建议您考虑将日期放在标题中,这样标题将不再有问题:删除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
}

现在设置标题的高度:

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

这是结果:

enter image description here