小编pet*_*ire的帖子

该addArrangedSubview代码有什么问题?

我试图了解addArrangedSubview是如何工作的,所以我尝试了一个虚拟项目。这是我的VC:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var stackOutlet: UIStackView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let newView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
    newView.backgroundColor = UIColor.darkGray

    let newView2 = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
    newView2.backgroundColor = UIColor.blue
    stackOutlet.addArrangedSubview(newView)
    stackOutlet.addArrangedSubview(newView2)
}

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


}
Run Code Online (Sandbox Code Playgroud)

启动应用程序时,没有任何显示。我想我必须在stackView中设置一些约束吗?

swift uistackview

2
推荐指数
1
解决办法
1713
查看次数

addArrangedSubview 并调整 StackView 的大小

我正在尝试使用按钮将视图添加到 StackView:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var stackOutlet: UIStackView!
    @IBOutlet weak var buttonOutlet: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    @IBAction func buttonAction(_ sender: AnyObject) {
        let newView = UIView(frame: CGRect(x: 0, y: 0, width: 240, height: 128))
        newView.heightAnchor.constraint(equalToConstant: 128)
        newView.widthAnchor.constraint(equalToConstant: 240)
        newView.backgroundColor = UIColor.green

        stackOutlet.addArrangedSubview(newView)
        stackOutlet.heightAnchor.constraint(equalToConstant: stackOutlet.frame.size.height …
Run Code Online (Sandbox Code Playgroud)

ios swift uistackview

2
推荐指数
1
解决办法
2759
查看次数

单元格未出现在UICollectionView中

我不明白为什么我的细胞没有显示:

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    let cellId = "pouet"

    lazy var customCollectionView : UICollectionView = {
        let layout = UICollectionViewLayout()
        let cv  = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.gray
        cv.dataSource = self
        cv.delegate = self

        return cv
    }()

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

        customCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
        addSubview(customCollectionView)
        addConstraintsWithFormat(format: "H:|[v0]|", views: customCollectionView)
        addConstraintsWithFormat(format: "V:|[v0]|", views: customCollectionView)
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: …
Run Code Online (Sandbox Code Playgroud)

uicollectionview uicollectionviewcell swift

1
推荐指数
1
解决办法
1947
查看次数