ios - UITableView 页脚的位置

标签 ios swift uitableview

我试图让用户插入他们的个人资料,我做到了。但我遇到了一个问题。

screenshot

页脚文本没有固定在页脚顶部,所以我一直在寻找解决方案,但找不到任何地方可以修复它。

我想像这张图片一样排列页脚文本

design image

我该怎么办?

titleForFooterInSection代码:

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    if section <= 1{
        return "5~15자로 작성해주세요"
    }else{
        return nil
    }
}

heightForFooterInSection+++代码:

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    switch section {
    case 0:
        return 44
    case 1:
        return 64
    default:
        return 0
    }
}

我想知道即使这样也会造成这样的问题。

最佳答案

如您所见,标题只是垂直居中对齐。所以如果你想将它对齐到顶部,你需要创建一个自定义页脚。代码可能如下所示。您应该根据自己的情况进行调整。

override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let view = UIView()
    view.backgroundColor = UIColor.lightGray

    let label = UILabel()
    // Note here you should use AutoLayout instead of the frame.
    // You may consider using SnapKit.
    label.frame = CGRect(x: 20, y: 8, width: 100, height: 10)

    if section <= 1 {
        label.text = "5~15자로 작성해주세요"
    }
    view.addSubview(label)
    return view
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    switch section {
    case 0:
        return 44
    case 1:
        return 64
    default:
        return 0
    }
}

关于ios - UITableView 页脚的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47047220/

相关文章:

ios - 将搜索字符串从 UISearchBar 转换为 MK MapView 地址

json - Firebase - 仅获取 JSON 结构的 "title"

ios - 如何将属性的类型限制为预定义的两种类型(UIColor 或 CAGradientLayer)

ios - UITableView 删除行在 iOS 中崩溃

objective-c - IOS动态设置分组TableView的标题高度

ios - SpriteKit 停止以指定角度旋转的轮子

iOS - 无法在 bundle 中加载 NIB

swift - 如何查明卡是否在 Braintree Payments 中被拒绝

ios - 当 UITableView 为空时显示图像

Javascript 和 CSS 移动友好全屏覆盖