Swift:使用程序大小的UIViews进行垂直滚动Feed

时间:2018-08-17 03:46:42

标签: ios swift uiview uiscrollview uikit

我正在使用从数据库(firebase)获取的数据在我的应用程序中构建滚动供稿。我对Swift的经验不是很丰富,因为我所做的大部分工作都是网页设计。我正在寻找的是一种确定UIViews高度的好方法。这是我目前拥有的(固定高度):enter image description here

这是我的代码UIView类:

class eventView: UIView {

let eventDate : UILabel = {
    let eventDate = UILabel()
    eventDate.translatesAutoresizingMaskIntoConstraints = false
    eventDate.numberOfLines = 0
    eventDate.textAlignment = .center
    return eventDate
}()
let eventTitle : UILabel = {
Same thing as eventDate
}()
let eventDesc : UILabel = {
same thing as eventDate
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    self.backgroundColor = UIColor.lightGray
    self.layer.cornerRadius = 10
    self.addSubview(eventDate)
    eventDate.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
    eventDate.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
    self.addSubview(eventTitle)
    eventTitle.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
    eventTitle.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
    eventTitle.topAnchor.constraint(equalTo: eventDate.bottomAnchor).isActive = true

    self.addSubview(eventDesc)
    eventDesc.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
    eventDesc.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
    eventDesc.topAnchor.constraint(equalTo: eventTitle.bottomAnchor).isActive = true


}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

这是显示这些UIView的我的列表:

                    var i = 0
                    for data in self.eventViewData {
                        let view = eventView(frame: CGRect(x: 0, y: ( CGFloat(170 * i)), width: self.scrollView.frame.width - 20, height: CGFloat(150)))
                        view.center.x = self.scrollView.center.x
                        view.eventDate.text = data.date
                        view.eventTitle.text = data.title
                        view.eventDesc.text = data.description
                        self.scrollView.addSubview(view)

                        i += 1
                    }

我通常使用HTML div,因此很难弄清楚如何设置样式。还应该提供任何有关如何在我的eventViews中以编程方式调整对UILabel约束的信息或指向教程的链接。

2 个答案:

答案 0 :(得分:1)

如何计算所需的高度似乎是个问题。 尽管有很多方法可以做到这一点,但是这样的事情会是这样的:

    func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
        let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.greatestFiniteMagnitude))
        label.numberOfLines = 0
        label.lineBreakMode = NSLineBreakMode.byWordWrapping
        label.font = font
        label.text = text
        label.sizeToFit()
        return label.frame.height
}

    let font = UIFont(name: "Helvetica", size: 20.0)
    var height = heightForView("This is just a load of text", font: font, width: 100.0)
    // apply height to desired view

我更愿意自己将heightForView函数放在扩展中,但这不是必需的。

快乐编码!

答案 1 :(得分:0)

我假设您使用的是表格视图,为什么不设置动态高度?很好的参考书https://www.html5rocks.com/en/tutorials/security/sandboxed-iframes/#granular-control-over-capabilities

本质上,您可以使用自动布局为单元格中的每个元素分别定义顶部和底部约束,并为heightforrow和estimateheightforrow使用表视图委托/数据源方法,返回UITableViewAutomaticDimension