ios - 分组的 UITableView 中的中心页脚 UILabel - Swift

标签 ios swift uitableview uilabel text-alignment

我目前正在向我的应用程序添加一个信息页面,并希望在分组的 UITableView 的页脚中心显示应用程序的版本号。我有它显示,但我不确定如何将它居中。

我看过使用 tableviews viewForFooterInSection;这是前进的正确方法,还是有更简单的方法?

非常感谢。

About screen with left aligned footer label

最佳答案

只需向您的 tableView 添加一个页脚,无论您有多少个部分,它都会放在底部

let tableViewFooter = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))
            tableViewFooter.backgroundColor = UIColor.greenColor()        
let version = UILabel(frame: CGRectMake(8, 15, tableView.frame.width, 30))
version.font = version.font.fontWithSize(14)
version.text = "Version 1.x"
version.textColor = UIColor.lightGrayColor()
version.textAlignment = .Center;

tableViewFooter.addSubview(version)

tableView.tableFooterView  = tableViewFooter

如果要为每个部分添加页脚

在您的 tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView 中创建一个 UIView()?方法并将标签添加到该 View 并将标签置于其中居中。

例子:

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView()

        let version = UILabel(frame: CGRectMake(8, 15, tableView.frame.width, 30))
        version.font = version.font.fontWithSize(14)
        version.text = "Version 1.x"
        version.textColor = UIColor.lightGrayColor()
        version.textAlignment = .Center;

        view.addSubview(version)

        return view
    }

关于ios - 分组的 UITableView 中的中心页脚 UILabel - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34070463/

相关文章:

ios - Xcode 6.1 错误条件绑定(bind)中的绑定(bind)值必须是可选类型

javascript - 如何重新定位固定的页眉和页脚?

ios - 使用输入参数初始化 xib

swift - 我可以在 Swift 中将默认类型分配给泛型类型 T 吗?

ios - 是否可以在 UICollectionView 下有一个 UITableView?

objective-c - UITableView.ScrollToTop - 改变它认为的 "top"

ios - 自定义 MapKit 注释滞后

ios - 控制台中的 Firebase Xcode 输出

ios - 如何在同一 View 上交替使用平移手势和滑动手势?

ios - 在 TableView 中对行重新排序后将对象保存在 UserDefaults 中