【问题标题】:UILabel Won't Pin To Bottom of NavBarUILabel 不会固定到导航栏的底部
【发布时间】:2021-01-05 00:43:39
【问题描述】:

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

    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)

更新

【问题讨论】:

  • 您可以像之前一样计算导航栏高度和机顶盒,但使用这个高度

标签: ios swift uinavigationcontroller uilabel


【解决方案1】:

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

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, 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")
        }
    }
}

这是结果

吉尔更新

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

结果如下:

【讨论】:

  • 但是当你滚动它时它也会进入正常大小的导航栏,有没有办法在向上滚动时删除它并缩回到正常的导航栏@Fabio
  • @GILL 我更新我的答案,如果满足您的问题,请检查它是正确的答案并投票:)
  • 检查我的更新,有没有办法不在小导航栏中显示“监视器”一词? @法比奥
  • @GILL 我不尝试这个解决方案,因为我的mac在家里,但是你可以尝试放入scrollViewWillBegin拖动这一行:navigationItem.title = “”和tableView willDisplay navigationItem.title = “监视器” ...让我知道它是否有效
  • 奇怪的是它对我来说不能正常工作,不知道为什么
猜你喜欢
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
  • 2020-06-24
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 2017-11-15
  • 1970-01-01
相关资源
最近更新 更多