【问题标题】:How can you constrain elements of a UITableViewCell to the bottom of the navigation bar?如何将 UITableViewCell 的元素限制在导航栏的底部?
【发布时间】:2019-11-29 17:24:32
【问题描述】:

我有一个半透明且清晰的导航栏来显示表格视图标题的完整图像,同时仍然有导航按钮。但是,我想将 UITableView 标题中的一个项目限制在清晰导航栏的底部,并且它无法正常工作。

我目前使用导航栏高度的负填充值将表格视图限制在最顶部

我尝试获取导航栏高度并将其传递给 UITableView 单元类,但即使在 tableView.reload() 之后它也不起作用

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cell = tableView.dequeueReusableCell(withIdentifier: "topCell") as! TopCell
        let navHeight = CGFloat(UIApplication.shared.statusBarFrame.size.height +
            (self.navigationController?.navigationBar.frame.height ?? 0.0))
        cell.navBarHeight = navHeight
        return cell
    }

var navBarHeight: CGFloat = 0

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        addSubview(icon)
        icon.anchor(top: topAnchor, left: leftAnchor, bottom: nil, right: nil, paddingTop: (CGFloat(navBarHeight + 10)), paddingLeft: 50, paddingBottom: 0, paddingRight: 0, width: 100, height: 100)
    }

当前结果将图标限制在最顶部。

【问题讨论】:

    标签: swift xcode uitableview uinavigationcontroller uikit


    【解决方案1】:

    使用导航栏访问视图控制器并从单元类中获取高度

    let mainVC = UINavigationController(rootViewController: MainViewController())
    let navBarHeight = CGFloat(mainVC.navigationBar.frame.height) + CGFloat(UIApplication.shared.statusBarFrame.size.height)
    

    【讨论】:

      猜你喜欢
      • 2018-01-16
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      相关资源
      最近更新 更多