如何使用Swift以编程方式设置视图的高度?

时间:2017-07-24 04:20:42

标签: ios swift uicollectionview

iOS新手在这里。我正在使用具有许多单元格的集合视图控制器,当我滚动到底部时,底部的一些单元格被切断(未显示)。

我尝试实施UICollectionViewDelegateFlowLayout协议。 我也尝试过设置 self.view.frame.size.height。但是,我只能将self.view.frame.size.height设置为小于200的值,但当我尝试超过默认大小时,屏幕不会变长。

我缺少一个默认约束吗?还会自动布局解决问题吗?

谢谢!

编辑:

我尝试添加约束 - 比如做self.view.leftAnchor.constraint(equalTo:self.view.leftAnchor,constant:8).isActive = true 但不确定“equalTo”的参数值应该是多少。我的目标是在视图控制器中为主视图添加约束。

这是我的代码 - 提前谢谢!

class ViewController: UICollectionViewController   {


override func viewDidLoad() {
    super.viewDidLoad()
    // self.clearsSelectionOnViewWillAppear = false


    self.view.frame.size.height = 1500
    // Register cell classes
    self.myCollectionView!.register(TableCell.self, forCellWithReuseIdentifier: "tableCell")



}




override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}



func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: 200)
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return 8
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "tableCell", for: indexPath) as! TableCell
    cell.contentView.backgroundColor = UIColor.blue

    return cell
}

}
class TableCell : UICollectionViewCell {

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

}
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

2 个答案:

答案 0 :(得分:0)

使用以下代码行,如果需要,可以通过编程方式提供约束

containerView.addSubview(inputTextField)
        //x,y,w,h
        inputTextField.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 8).isActive = true
        inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        inputTextField.rightAnchor.constraint(equalTo: sendButton.leftAnchor).isActive = true
        inputTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true

如果您使用故事板进行约束,那么它将解决您的问题[简单方法]

答案 1 :(得分:0)

试用此代码 - :

      class ViewController: UIVIewController,UICollectionViewDataSource,UICollectionViewDelegate   {


        override func viewDidLoad() {
            super.viewDidLoad()
            // Register cell classes
            setUPView()
   }

        // CREATE COLLECTION VIEW

            lazy var collectionView : UICollectionView = {

            let layout = UICollectionViewFlowLayout()
            var collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
            collectionView.delegate = self
            collectionView.datasource = self
            collectionView.backgroundColor = UIColor.yellow
            return collectionView

            }()

            func setUPView(){
        // ADD COLLECTION VIEW ON VIEW
            view.addSubview(collectionView)
        collectionView.register(TableCell.self, forCellWithReuseIdentifier: "tableCell")
        // SET UP CONSTRAINTS
            collectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
            collectionView.topYAnchor.constraint(equalTo:view.topAnchor,constant:64).isActive = true
                collectionView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
                collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true


        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }



       func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 1
        }



        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize(width: view.frame.width, height: 200)
        }

       func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            // #warning Incomplete implementation, return the number of items
            return 8
        }

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "tableCell", for: indexPath) as! TableCell
            cell.contentView.backgroundColor = UIColor.blue

            return cell
        }

     }