【问题标题】:How to center a custom UIView in a TableView?如何在 TableView 中居中自定义 UIView?
【发布时间】:2017-03-12 01:31:58
【问题描述】:

我试图在 UITableView 中居中自定义视图,它工作正常,但是当我滚动时它不会更新到 tableview 的中心,它只是停留在顶部。这是我的代码:

 let customView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 75))
    customView.backgroundColor = UIColor(red: 0/255, green: 174/255, blue: 250/255, alpha: 1.0)
    let label = UILabel(frame: CGRect(x: customView.frame.midX, y: customView.frame.midY, width: 150, height: 75))
    label.text = "Text"
    label.textAlignment = .center
    label.font = UIFont(name: "HelveticaNeue-Light", size: 18)
    label.textColor = UIColor.white
    customView.addSubview(label)
    label.center = customView.center
    customView.layer.cornerRadius = 4.0

    self.view.addSubview(customView)
    customView.center = CGPoint(x: self.tableView.bounds.width/2, y: self.tableView.bounds.height/2)

    UIView.animate(withDuration: 1.5, animations: {
        customView.alpha = 0.0
    }) { (success: Bool) in
        customView.removeFromSuperview()
    }

关于如何完成这项工作的任何想法?谢谢。

【问题讨论】:

  • 该代码中的self 是什么?您希望发生的事情没有发生?

标签: swift uitableview uiview scrollview uiviewanimation


【解决方案1】:

在tableView中添加customView而不是在视图中。

self.tableView.addSubview(customView)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多