Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以编程方式添加约束_Ios_Swift_Constraints - Fatal编程技术网

Ios 以编程方式添加约束

Ios 以编程方式添加约束,ios,swift,constraints,Ios,Swift,Constraints,请我需要以下方面的帮助,我刚开始学习如何以编程方式设计界面,在几次教程之后,我想尝试一些东西,然后我就被绊倒了 我正在尝试实现下面的图像 但这就是我得到的 下面是我的代码 class FeedsCell: UICollectionViewCell{ override init(frame: CGRect){ super.init(frame: frame) setupViews() } let thumNailImageView : UIImageView = {

请我需要以下方面的帮助,我刚开始学习如何以编程方式设计界面,在几次教程之后,我想尝试一些东西,然后我就被绊倒了

我正在尝试实现下面的图像

但这就是我得到的

下面是我的代码

class FeedsCell: UICollectionViewCell{

override init(frame: CGRect){
    super.init(frame: frame)
    setupViews()
}

let thumNailImageView : UIImageView = {
    let imageView = UIImageView()
    imageView.backgroundColor = UIColor.blue
    return imageView
}()

let sourceName:UILabel = {
  let srcLabel = UILabel()
    srcLabel.backgroundColor = UIColor.purple
    srcLabel.translatesAutoresizingMaskIntoConstraints = false
    return srcLabel
}()

let separatorView: UIView = {
   let view = UIView()
    view.backgroundColor = UIColor.black
    return view
}()

func setupViews(){
    addSubview(thumNailImageView)
    addSubview(separatorView)
    addSubview(sourceName)

    addConstraintsWithFormat(format: "H:|-16-[v0(194)]", views: thumNailImageView)

    addConstraintsWithFormat(format: "V:|-16-[v0]-16-[v1(1)]|", views: thumNailImageView, separatorView)

    addConstraintsWithFormat(format: "H:|[v0]|", views: separatorView)



    //left Constriants
    addConstraint(NSLayoutConstraint(item: sourceName, attribute: .left, relatedBy: .equal, toItem: thumNailImageView, attribute: .right, multiplier: 1, constant: 8))

    //Right constraints
    addConstraint(NSLayoutConstraint(item: sourceName, attribute: .right, relatedBy: .equal, toItem: thumNailImageView, attribute: .right, multiplier: 1, constant: 0))

    addConstraintsWithFormat(format: "H:|-8-[v0]-8-|", views: sourceName)
    addConstraintsWithFormat(format: "V:|-8-[v0(20)]", views: sourceName)

}

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

}


extension UIView{
    func addConstraintsWithFormat(format: String, views: UIView...){

        var viewDictionary = [String: UIView]()
        for(index, view) in views.enumerated(){
            let key = "v\(index)"
            view.translatesAutoresizingMaskIntoConstraints = false
            viewDictionary[key] = view
        }


    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewDictionary))
    }
}

我已经能够使用SnapKit解决这个问题,为了实现图像,我做了类似的事情

let screenFrame = UIScreen.main.bounds

    thumNailImageView.snp.makeConstraints { (make) in
        make.width.equalTo(194)
        make.top.equalTo(contentView).offset(20)
        make.left.equalTo(contentView).offset(20)
        make.bottom.equalTo(contentView).offset(-20)
    }

    sourceName.snp.makeConstraints { (make) in
        make.width.equalTo(screenFrame.width/2 - 40 )
        make.height.equalTo(20)
        make.top.equalTo(contentView).offset(20)
        make.left.equalTo(contentView).offset(screenFrame.width/2 + 20 )
    }

    postTitle.snp.makeConstraints { (make) in
        make.width.equalTo(screenFrame.width/2 - 40 )
        make.height.equalTo(30)
        make.top.equalTo(sourceName).offset(30)
        make.left.equalTo(contentView).offset(screenFrame.width/2 + 20 )
    }

    timeStamp.snp.makeConstraints { (make) in
        make.width.equalTo(screenFrame.width/2 - 40 )
        make.height.equalTo(10)
        make.top.equalTo(postTitle).offset(40)
        make.left.equalTo(contentView).offset(screenFrame.width/2 + 20 )
    }

    postContent.snp.makeConstraints { (make) in
        make.width.equalTo(screenFrame.width/2 - 40 )
        make.height.equalTo(60)
        make.top.equalTo(timeStamp).offset(20)
        make.left.equalTo(contentView).offset(screenFrame.width/2 + 20 )
    }

我已经能够使用SnapKit解决这个问题,为了实现图像,我做了类似的事情

let screenFrame = UIScreen.main.bounds

    thumNailImageView.snp.makeConstraints { (make) in
        make.width.equalTo(194)
        make.top.equalTo(contentView).offset(20)
        make.left.equalTo(contentView).offset(20)
        make.bottom.equalTo(contentView).offset(-20)
    }

    sourceName.snp.makeConstraints { (make) in
        make.width.equalTo(screenFrame.width/2 - 40 )
        make.height.equalTo(20)
        make.top.equalTo(contentView).offset(20)
        make.left.equalTo(contentView).offset(screenFrame.width/2 + 20 )
    }

    postTitle.snp.makeConstraints { (make) in
        make.width.equalTo(screenFrame.width/2 - 40 )
        make.height.equalTo(30)
        make.top.equalTo(sourceName).offset(30)
        make.left.equalTo(contentView).offset(screenFrame.width/2 + 20 )
    }

    timeStamp.snp.makeConstraints { (make) in
        make.width.equalTo(screenFrame.width/2 - 40 )
        make.height.equalTo(10)
        make.top.equalTo(postTitle).offset(40)
        make.left.equalTo(contentView).offset(screenFrame.width/2 + 20 )
    }

    postContent.snp.makeConstraints { (make) in
        make.width.equalTo(screenFrame.width/2 - 40 )
        make.height.equalTo(60)
        make.top.equalTo(timeStamp).offset(20)
        make.left.equalTo(contentView).offset(screenFrame.width/2 + 20 )
    }

稍微有点过时,但是您是否考虑过使用较新的anchors API?我发现做这种事情容易多了。可能是上面的一个简单错误(我乍一看并没有看到)就会消失。例如,
sourceName.leftAnchor.constrainEqualToAnchor(thumNailImageView.rightAnchor)。isActive=true
@TravisGriggs一个更好的解决方案是使用xibs和故事板。@Sulthan我可以使用xibs和故事板来实现它,但我正试图通过编程实现这一点,因为我会表演一些functionalities@TravisGriggs我正在研究AnchorAPI,谢谢。有点过时了,但是你考虑过使用更新的AnchorAPI吗?我发现做这种事情容易多了。可能是上面的一个简单错误(我乍一看并没有看到)就会消失。例如,
sourceName.leftAnchor.constrainEqualToAnchor(thumNailImageView.rightAnchor)。isActive=true
@TravisGriggs一个更好的解决方案是使用xibs和故事板。@Sulthan我可以使用xibs和故事板来实现它,但我正试图通过编程实现这一点,因为我会表演一些functionalities@TravisGriggs我正在查看锚api,谢谢。