应用NSLayoutConstraint时,iCarousel“cards”消失

时间:2017-08-11 06:33:02

标签: ios swift nslayoutconstraint icarousel

我目前正在与iCarousel合作 https://github.com/nicklockwood/iCarousel我的项目。目标是提出一个spotify封面流UI。

我遇到以下代码的问题:

func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
    let tempView = UIView(frame: CGRect(x: 0, y: 0, width: 350, height: 350))
    tempView.layer.cornerRadius = 10
    tempView.layer.masksToBounds = true
    tempView.translatesAutoresizingMaskIntoConstraints = false
    tempView.backgroundColor = .white

    var uiimage = UIImageView()



    uiimage.image = #imageLiteral(resourceName: "songAlbumCover")
    uiimage.translatesAutoresizingMaskIntoConstraints = false
    var temviewsmaller = UIView(frame: CGRect(x: 0, y: 0, width: 350, height: 350))
    temviewsmaller.backgroundColor = .yellow
    temviewsmaller.addSubview(uiimage)

    // When I included the following constraints the "cards" disappear why??
    uiimage.topAnchor.constraint(equalTo: temviewsmaller.topAnchor).isActive = true
    uiimage.bottomAnchor.constraint(equalTo: temviewsmaller.bottomAnchor).isActive = true
    uiimage.leftAnchor.constraint(equalTo: temviewsmaller.leftAnchor, constant: 10).isActive = true
    uiimage.rightAnchor.constraint(equalTo: temviewsmaller.rightAnchor, constant: 10).isActive = true


    tempView.addSubview(temviewsmaller)
    return tempView
}

iCarousel和NSLayoutConstraint有问题吗?我试图在标签对象和图像对象等“卡片”上放置更多信息。但是因为我没有使用故事板并以编程方式执行所有操作。一直在寻找关于此的在线教程,但没有运气:(

如果没有应用约束,卡片将正确显示。但我最终需要约束,因为我正在考虑添加更多的子视图,例如UILabel等。

更新

    func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {

    print(carousel.contentView.center.y)
    print(carousel.contentView.center.x)

    let tempView = UIView()
    tempView.layer.cornerRadius = 10
    tempView.layer.masksToBounds = true
    tempView.translatesAutoresizingMaskIntoConstraints = false
    tempView.backgroundColor = .white

    var uiimage = UIImageView()



    uiimage.image = #imageLiteral(resourceName: "yeezy")
    uiimage.translatesAutoresizingMaskIntoConstraints = false
    var temviewsmaller = UIView()
    temviewsmaller.backgroundColor = .yellow
    temviewsmaller.addSubview(uiimage)

    // When I included the following constraints the "cards" disappear why??
    uiimage.topAnchor.constraint(equalTo: temviewsmaller.topAnchor).isActive = true
    uiimage.bottomAnchor.constraint(equalTo: temviewsmaller.bottomAnchor).isActive = true
    uiimage.leftAnchor.constraint(equalTo: temviewsmaller.leftAnchor, constant: 10).isActive = true
    uiimage.rightAnchor.constraint(equalTo: temviewsmaller.rightAnchor, constant: 10).isActive = true

    tempView.addSubview(temviewsmaller)

    temviewsmaller.topAnchor.constraint(equalTo: tempView.topAnchor).isActive = true
    temviewsmaller.bottomAnchor.constraint(equalTo: tempView.bottomAnchor).isActive = true
    temviewsmaller.leftAnchor.constraint(equalTo: tempView.leftAnchor, constant: 10).isActive = true
    temviewsmaller.rightAnchor.constraint(equalTo: tempView.rightAnchor, constant: 10).isActive = true

    carousel.contentView.addSubview(tempView)
    tempView.topAnchor.constraint(equalTo: carousel.contentView.topAnchor).isActive = true
    tempView.bottomAnchor.constraint(equalTo: carousel.contentView.bottomAnchor).isActive = true
    tempView.leftAnchor.constraint(equalTo: carousel.contentView.leftAnchor, constant: 10).isActive = true
    tempView.rightAnchor.constraint(equalTo: carousel.contentView.rightAnchor, constant: 10).isActive = true


    return carousel.contentView
}

0 个答案:

没有答案