Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CollectionView在StackView中消失(Swift)_Ios_Swift_Uicollectionview_Uicollectionviewlayout_Uistackview - Fatal编程技术网

Ios CollectionView在StackView中消失(Swift)

Ios CollectionView在StackView中消失(Swift),ios,swift,uicollectionview,uicollectionviewlayout,uistackview,Ios,Swift,Uicollectionview,Uicollectionviewlayout,Uistackview,我试图实现在图中显示的StaveVIEW配置,但是由于某种原因,当使用Access发行版时,包含CopyVIEW的顶部堆栈消失了。 stackView.distribution = .fill (stack containing collectionView disappears) stackView.distribution = .fillEqually (collectionView appears fine in stackView) 我已经为此挣扎了好几天,你会在我注释掉

我试图实现在图中显示的StaveVIEW配置,但是由于某种原因,当使用Access发行版

时,包含CopyVIEW的顶部堆栈消失了。
stackView.distribution = .fill        (stack containing collectionView disappears)
stackView.distribution = .fillEqually (collectionView appears fine in stackView)
我已经为此挣扎了好几天,你会在我注释掉的部分中看到残留物:设置压缩阻力/拥抱优先级,尝试更改固有高度,更改UICollectionViewFlowLayout()的.layout.itemSize,等等。。。在我手里什么都不管用。如果您只需将其粘贴到中并将其与空UIViewController关联,则此处的代码将运行。顶部的collectionView堆栈包含一个pickerView,下面的堆栈是PageControlView、按钮子堆栈和UIView。它在.fillEqualized发行版中运行良好,因此这纯粹是一个布局问题。非常感谢

// CodeStackVC2
// Test of programmatically generated stack views
// Output: nested stack views
// To make it run:
// 1) Assign the blank storyboard VC as CodeStackVC2
// 2) Move the "Is Inital VC" arrow to the blank storyboard

import UIKit

class CodeStackVC2: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate {

  let fruit = ["Apple", "Orange", "Plum", "Qiwi", "Banana"]
  let veg = ["Lettuce", "Carrot", "Celery", "Onion", "Brocolli"]
  let meat = ["Beef", "Chicken", "Ham", "Lamb"]
  let bread = ["Wheat", "Muffin", "Rye", "Pita"]
  var foods = [[String]]()
  let button = ["bread","fruit","meat","veg"]

  var sView = UIStackView()
  let cellId = "cellId"

  override func viewDidLoad() {
    super.viewDidLoad()
    foods = [fruit, veg, meat, bread]
    setupViews()
  }

  //MARK: Views
  lazy var cView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    layout.minimumLineSpacing = 0
    layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
    layout.itemSize = CGSize(width: self.view.frame.width, height: 120)
    let cv = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
     cv.backgroundColor = UIColor.lightGray
    cv.isPagingEnabled = true
    cv.dataSource = self
    cv.delegate = self
    cv.isUserInteractionEnabled = true
    //    var intrinsicContentSize: CGSize {
    //      return CGSize(width: UIViewNoIntrinsicMetric, height: 120)
    //    }
    return cv
  }()

  lazy var pageControl: UIPageControl = {
    let pageC = UIPageControl()
    pageC.numberOfPages = self.foods.count
    pageC.pageIndicatorTintColor = UIColor.darkGray
    pageC.currentPageIndicatorTintColor = UIColor.white
    pageC.backgroundColor = .lightGray
    pageC.addTarget(self, action: #selector(changePage(sender:)), for: UIControlEvents.valueChanged)
//    pageC.setContentHuggingPriority(900, for: .vertical)
//    pageC.setContentCompressionResistancePriority(100, for: .vertical)
    return pageC
  }()

  var readerView: UIView = {
    let rView = UIView()
    rView.backgroundColor = UIColor.brown
//    rView.setContentHuggingPriority(100, for: .vertical)
//    rView.setContentCompressionResistancePriority(900, for: .vertical)
    return rView
  }()


  func makeButton(_ name:String) -> UIButton{
    let newButton = UIButton(type: .system)
    let img = UIImage(named: name)?.withRenderingMode(.alwaysTemplate)
    newButton.setImage(img, for: .normal)
    newButton.contentMode = .scaleAspectFit
    newButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleButton)))
    newButton.isUserInteractionEnabled = true
    newButton.backgroundColor = .orange
    return newButton
  }
  //Make a 4-item vertical stackView containing
  //cView,pageView,subStackof 4-item horiz buttons, readerView
  func setupViews(){
    cView.register(FoodCell.self, forCellWithReuseIdentifier: cellId)
    //generate an array of buttons
    var buttons = [UIButton]()
    for i in 0...foods.count-1 {
      buttons += [makeButton(button[i])]
    }
    let subStackView = UIStackView(arrangedSubviews: buttons)
    subStackView.axis = .horizontal
    subStackView.distribution = .fillEqually
    subStackView.alignment = .center
    subStackView.spacing = 20
    //set up the stackView
    let stackView = UIStackView(arrangedSubviews: [cView,pageControl,subStackView,readerView])
    stackView.axis = .vertical
    //.fillEqually works, .fill deletes cView, .fillProportionally & .equalSpacing delete cView & readerView
    stackView.distribution = .fillEqually
    stackView.alignment = .fill
    stackView.spacing = 5
    //Add the stackView using AutoLayout
    view.addSubview(stackView)
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 5).isActive = true
    stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  }

  func handleButton() {
    print("button pressed")
  }
  //pageControl page changer
  func changePage(sender: AnyObject) -> () {
    let x = CGFloat(pageControl.currentPage) * cView.frame.size.width
    cView.setContentOffset(CGPoint(x:x, y:0), animated: true)
  }

  //MARK: horizontally scrolling Chapter collectionView
  func scrollViewDidScroll(_ scrollView: UIScrollView) {
    //    let scrollBarLeft = CGFloat(scrollView.contentOffset.x) / CGFloat(book.chap.count + 1)
    //    let scrollBarWidth = CGFloat( menuBar.frame.width) / CGFloat(book.chap.count + 1)
  }

  func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    let index = targetContentOffset.pointee.x / view.frame.width
    pageControl.currentPage = Int(index) //change PageControl indicator
  }

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

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! FoodCell
    cell.foodType = foods[indexPath.item]
    return cell
  }

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

class FoodCell:UICollectionViewCell, UIPickerViewDelegate, UIPickerViewDataSource {

  var foodType: [String]? {
    didSet {
      pickerView.reloadComponent(0)
      pickerView.selectRow(0, inComponent: 0, animated: true)
    }
  }

  lazy var pickerView: UIPickerView = {
    let pView = UIPickerView()
    pView.frame = CGRect(x:0,y:0,width:Int(pView.bounds.width), height:Int(pView.bounds.height))
    pView.delegate = self
    pView.dataSource = self
    pView.backgroundColor = .darkGray
    return pView
  }()

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

  func setupViews() {
    backgroundColor = .clear
    addSubview(pickerView)
    addConstraintsWithFormat("H:|[v0]|", views: pickerView)
    addConstraintsWithFormat("V:|[v0]|", views: pickerView)
  }
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
  }

  func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if let count = foodType?.count {
      return count
    } else {
      return 0
    }
  }

  func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    let pickerLabel = UILabel()
    pickerLabel.font = UIFont.systemFont(ofSize: 15)
    pickerLabel.textAlignment = .center
    pickerLabel.adjustsFontSizeToFitWidth = true
    if let foodItem = foodType?[row] {
      pickerLabel.text = foodItem
      pickerLabel.textColor = .white
      return pickerLabel
    } else {
      print("chap = nil in viewForRow")
      return UIView()
    }
  }


}
//CodeStackVC2
//测试以编程方式生成的堆栈视图
//输出:嵌套堆栈视图
//要使其运行:
// 1)将空白故事板VC分配为CODISTACKVC2
// 2)将“初始VC”箭头移动到空白故事板
导入UIKit
类代码StackVC2:UIViewController、UICollectionViewDataSource、UICollectionViewDelegate、UICollectionViewDelegateFlowLayout、UIGestureRecognitizerDelegate{
让水果=[“苹果”、“橘子”、“李子”、“七叶树”、“香蕉”]
让蔬菜=[“生菜”、“胡萝卜”、“芹菜”、“洋葱”、“花椰菜”]
让肉=[“牛肉”、“鸡肉”、“火腿”、“羊肉”]
让面包=[“小麦”、“松饼”、“黑麦”、“皮塔”]
var foods=[[String]]()
let button=[“面包”、“水果”、“肉”、“蔬菜”]
var sView=UIStackView()
let cellId=“cellId”
重写func viewDidLoad(){
super.viewDidLoad()
食品=[水果、蔬菜、肉、面包]
设置视图()
}
//标记:视图
lazy var cView:UICollectionView={
let layout=UICollectionViewFlowLayout()
layout.scrollDirection=.horizontal
layout.minimumLineSpacing=0
layout.sectionInset=UIEDGEINSET(顶部:5,左侧:5,底部:5,右侧:5)
layout.itemSize=CGSize(宽度:self.view.frame.width,高度:120)
让cv=UICollectionView(帧:self.view.frame,collectionViewLayout:layout)
cv.backgroundColor=UIColor.lightGray
cv.isPaginEnabled=true
cv.dataSource=self
cv.delegate=self
cv.isUserInteractionEnabled=true
//var intrinsicContentSize:CGSize{
//返回CGSize(宽度:UIViewNoIntrinsicMetric,高度:120)
//    }
返回cv
}()
惰性变量pageControl:UIPageControl={
设pageC=UIPageControl()
pageC.numberOfPages=self.foods.count
pageC.pageIndicatorTintColor=UIColor.darkGray
pageC.currentPageIndicatorTintColor=UIColor.white
pageC.backgroundColor=浅灰色
pageC.addTarget(自身,操作:#选择器(更改页面(发件人:)),用于:UIControlEvents.valueChanged)
//pageC.setContentHuggingPriority(900,用于:。垂直)
//pageC.setContentCompressionResistancePriority(100,用于:。垂直)
返回第c页
}()
变量readerView:UIView={
让rView=UIView()
rView.backgroundColor=UIColor.brown
//rView.setContentHuggingPriority(100,用于:。垂直)
//rView.setContentCompressionResistancePriority(900,用于:。垂直)
返回视图
}()
func makeButton(u名称:String)->UIButton{
让newButton=UIButton(类型:。系统)
让img=UIImage(命名:name)?.withRenderingMode(.alwaysTemplate)
setImage(img,用于:。正常)
newButton.contentMode=.ScaleSpectFit
newButton.addGestureRecognizer(UITapGestureRecognizer(目标:self,操作:#选择器(把手按钮)))
newButton.isUserInteractionEnabled=true
newButton.backgroundColor=.orange
返回纽扣
}
//制作一个4项垂直堆叠视图,其中包含
//cView、pageView、4项水平按钮子组、readerView
func setupViews(){
cView.register(FoodCell.self,forCellWithReuseIdentifier:cellId)
//生成一个按钮数组
变量按钮=[UIButton]()
对于0…食物中的i。计数-1{
按钮+=[makeButton(按钮[i])]
}
let subStackView=UIStackView(排列子视图:按钮)
subStackView.axis=.horizontal
subStackView.distribution=.FillView
subStackView.alignment=.center
subStackView.SPACE=20
//设置stackView
让stackView=UIStackView(排列的子视图:[cView,pageControl,subStackView,readerView])
stackView.axis=.vertical
//.fill同样有效,.fill删除cView,.fill按比例和.Equalspace删除cView和readerView
stackView.distribution=.FillView
stackView.alignment=.fill
stackView.spacing=5
//使用AutoLayout添加stackView
view.addSubview(stackView)
stackView.TranslatesAutoResizezingMaskintoConstraints=false
stackView.topAnchor.constraint(equalTo:view.topAnchor,常量:5)。isActive=true
stackView.leadingAnchor.constraint(等式:view.leadingAnchor).isActive=true
stackView.trailingAnchor.constraint(equalTo:view.trailingAnchor).isActive=true
stackView.bottomAnchor.constraint(equalTo:view.bottomAnchor).isActive=true
}
func把手按钮(){
打印(“按下按钮”)
}
//页面控制页面转换器
func changePage(发送方:AnyObject)->(){
设x=CGFloat(pageControl.currentPage)*cView.frame.size.width
cView.setContentOffset(CGPoint(x:x,y:0),动画:true)
}
//标记:水平滚动章节集合视图
func scrollViewDidScroll(scrollView:UIScrollView){
//让scrollBarLeft=CGFloat(scrollView.contentOffset.x)/CGFloat(book.chap.count+1)
//让scrollBarWidth=CGFloat(menuBar.frame.width)/CGFloat(book.chap.count+1)
}
func ScrollViewWillendDraging(scrollView:UIScrollView,withVelocity:CGPoint,targetContentOffset:UnsafemeutablePointer){
让index=targetContentOffset.pointee.x/view.frame.width
pageControl.currentPage=Int(index)//更改pageControl指示符
}
func collectionView(collectionView:UICollectionV
NSLayoutConstraint.activate([
  cView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25)
])
class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    setupViews()
  }

  private func setupViews() {

    let blueView = UIView()
    blueView.backgroundColor = .blue

    let titleLabel = UILabel()
    titleLabel.text = "Hello"

    let button = UIButton(type: .system)
    button.setTitle("Action", for: .normal)

    let redView = UIView()
    redView.backgroundColor = .red

    let stackView = UIStackView(arrangedSubviews: [blueView, titleLabel, button, redView])
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.axis = .vertical
    stackView.spacing = 8
    view.addSubview(stackView)

    NSLayoutConstraint.activate([
      stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
      stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
      stackView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor),
      stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
      blueView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25)
      ])
  }
}