2016-06-26 3 views
0

Я добавил UICollectionView в качестве подсмотра в другом контроллере. Я установил для него рамку, а поток UICollectionView - горизонтальный. Мне нужно показать только 1 ряд элементов и прокрутку по горизонтали. Но я получаю несколько строк по вертикали. Я пробовал несколько разных методов, но это мне не помогает. Я искал информацию об этом, но эти методы также мне не помогают. Как я могу это исправить?Неправильное поведение UICollectionView как Subview

Мой код

override func viewDidLoad() { 
    super.viewDidLoad() 
    addUIElements() 
} 

// MARK: - UI functions 

private func addUIElements() { 
    view.addSubview(headerView) 

    let lifelineController = StoryboardManager.lifeLineStoryboard.instantiateViewControllerWithIdentifier("LifeLineCollectionViewController") as! LifeLineCollectionViewController 
    addChildViewController(lifelineController) 
    lifelineController.view.frame = CGRect(x: 0, y: headerView.frame.height, width: headerView.frame.width, height: 100) 
    view.addSubview(lifelineController.view) 
    lifelineController.didMoveToParentViewController(self) 
} 

код Компоновка

extension LifeLineCollectionViewController: UICollectionViewDelegateFlowLayout { 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
     let value = 50 
     return CGSize(width: value, height: value) 
    } 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat { 
     return 8 
    } 

// func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
//  return CGSize(width: collectionView.frame.width, height: 376) 
// } 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { 
     let top: CGFloat = 48 
     let cornerValue: CGFloat = 8 
     let bottom: CGFloat = 40 
     return UIEdgeInsets(top: top, left: cornerValue, bottom: bottom, right: cornerValue) 
    } 
} 

Мой результат enter image description here

я напечатал в UICollectionView в viewDidAppear его рамку, и я получаю совершенно иной результат 508 высоты, но я установите 136 на высоту.

+0

Что расположение, высоты рамы, высота элемента и значения вставок? – Wain

+0

@Wain, пожалуйста, забейте по макету – Alexander

ответ

0

я решил его с помощью SnapKit

private func addUIElements() { 
    view.addSubview(headerView) 

    let lifelineController = StoryboardManager.lifeLineStoryboard.instantiateViewControllerWithIdentifier("LifeLineCollectionViewController") as! LifeLineCollectionViewController 
    addChildViewController(lifelineController) 
    view.addSubview(lifelineController.view) 
    lifelineController.view.snp_makeConstraints { (make) in 
     make.width.equalTo(headerView.frame.width) 
     make.height.equalTo(136) 
     make.centerX.equalTo(view.snp_centerX) 
     make.top.equalTo(headerView.snp_bottom) 
    } 
    lifelineController.didMoveToParentViewController(self) 
} 
Смежные вопросы