私はIOSの開発に新しいです。UICollectionViewをプログラム上でコンテナに追加するにはどうすればよいですか?
以下は、私が を実装したいものです - これは、コードでのUITableView
を追加し、第二の容器で - UICollectionView を追加し、第一の容器で - メインビュー に2個のコンテナを持っています私がこれまで行ってきたこと。
メインビュー:
class MainViewController:UIViewController {
let itemView:UIView = {
let itemContainerView = UIView()
itemContainerView.backgroundColor = UIColor.lightGray
let collectionView = CollectionViewController()
itemContainerView.addSubview(collectionView.view)
itemContainerView.translatesAutoresizingMaskIntoConstraints = false
return itemContainerView
}()
let tableView:UIView = {
let tableContainerView = UIView()
tableContainerView.backgroundColor = UIColor.gray
tableContainerView.translatesAutoresizingMaskIntoConstraints = false
return tableContainerView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
view.addSubview(itemView)
view.addSubview(tableView)
setupItemView()
setupTableView()
}
func setupItemView(){
itemView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
itemView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
itemView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 2/3).isActive = true
itemView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
}
func setupTableView() {
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.leftAnchor.constraint(equalTo: itemView.rightAnchor).isActive = true
tableView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1/3).isActive = true
tableView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
}
}
CollectionViewController:
class CollectionViewController:UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
let cellId = "CellId"
var colView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 111, height: 111)
colView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
colView.delegate = self
colView.dataSource = self
colView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
colView.backgroundColor = UIColor.white
colView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(colView)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
cell.backgroundColor = UIColor.brown
return cell
}
}
私は私のメイン画面は、2個のコンテナを持って見ることができましたが、10個の収集細胞を見ることができませんでした。
誰かが正しい方向に助言できますか?前もって感謝します。私は最初のビューとしてMainViewControllerを設定すると
UPDATED
、それが正常に動作しています。 しかし、私のプロジェクトでは、MainViewControllerは最初のビューではありません。 フローは次のとおりです。 1.ユーザログイン 2.ボタン付きダッシュボードが表示されます 3.ボタンをクリックすると、ユーザがMainViewControllerにナビゲートされます。ここで
は私のダッシュボードクラスでは、ビューがロードされた後、いくつかの点でreloadData
を呼び出すようにしてください
class DashboardController: UIViewController{
let orderBtn:UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor.init(red: 173/255, green: 184/255, blue: 255/255, alpha: 1)
button.layer.cornerRadius = 5
button.setTitle("Select Item" , for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(handleOrderNavigation), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
view.addSubview(orderBtn)
setupOrderBtn()
}
func handleOrderNavigation() {
let mainViewController= MainViewController()
let mainViewNavController= UINavigationController(rootViewController: mainViewController)
self.present(mainViewNavController, animated: false, completion: nil)
}
}
collectionViewが 'viewDidLoad'に正しいフレームを持っているのではないかと疑います。 autolayoutを使用するか、後でフレームを設定してください。また、CollectionViewControllerを 'itemView'に追加するときにフレームを設定しません。 –
@JamesP autolayoutを使ってビューをスーパービュー(コンテナ)に合わせるにはどうすればよいですか? –