首页 文章

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

提问于
浏览
-1

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

    使用以下代码行,您可以根据需要以编程方式提供约束

    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
    

    如果你使用故事板作为约束,是的,它将解决你的问题[简单方法]

  • 0

    Try this code-:

    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
            }
    
         }
    

相关问题