【问题标题】:How can I programmatically set the view's height with Swift?如何使用 Swift 以编程方式设置视图的高度?
【发布时间】:2017-12-29 13:35:15
【问题描述】:

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

【问题讨论】:

  • 检查集合视图的约束。如果集合视图覆盖完整的控制器高度和宽度,则为集合视图提供前导、尾随、顶部、底部约束。如果您嵌入了标签栏或导航栏以及根据管理框架并给出相同的约束。
  • 如何通过顶部的导航栏添加约束?
  • 您从情节提要或以编程方式给出约束?
  • 以编程方式
  • 显示。您的。代码。您不对集合视图的单元格使用约束,因此听起来您做错了什么。解释集合视图如何进入界面。您没有提供任何可以让任何人帮助您的信息。

标签: ios swift uicollectionview


【解决方案1】:

试试这个代码-:

      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
        }

     }

【讨论】:

    【解决方案2】:

    如果需要,您可以使用以下代码行以编程方式给出约束

    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
    

    如果您使用情节提要进行约束,是的,它将解决您的问题 [简单方法]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 2021-04-23
      相关资源
      最近更新 更多