Ios 当添加到UIStackView或UIScrollView时,集合视图将消失
我在我的IOS应用程序中进行了这个设计,我想要的最终设计是,我已经有了一行的集合视图,如图a所示,故事板是这样的 只是当我将集合视图放在堆栈视图或滚动视图中以向视图添加类似的集合视图时,它要么消失,要么只显示一半?这就是它在应用程序中的样子在这个,故事板看起来像 为什么我做错了?一旦集合视图添加到堆栈视图或滚动视图中,它就会更改。用我目前构建的集合视图实现我在本文中展示的设计的最佳方式是什么 这是当前视图的代码:Ios 当添加到UIStackView或UIScrollView时,集合视图将消失,ios,swift,uicollectionview,uistackview,Ios,Swift,Uicollectionview,Uistackview,我在我的IOS应用程序中进行了这个设计,我想要的最终设计是,我已经有了一行的集合视图,如图a所示,故事板是这样的 只是当我将集合视图放在堆栈视图或滚动视图中以向视图添加类似的集合视图时,它要么消失,要么只显示一半?这就是它在应用程序中的样子在这个,故事板看起来像 为什么我做错了?一旦集合视图添加到堆栈视图或滚动视图中,它就会更改。用我目前构建的集合视图实现我在本文中展示的设计的最佳方式是什么 这是当前视图的代码: import UIKit import AVKit import Firebase
import UIKit
import AVKit
import Firebase
import MMPlayerView
class HomeViewController: UIViewController {
var offsetObservation: NSKeyValueObservation?
lazy var mmPlayerLayer: MMPlayerLayer = {
let l = MMPlayerLayer()
l.cacheType = .memory(count: 5)
l.coverFitType = .fitToPlayerView
l.videoGravity = AVLayerVideoGravity.resizeAspect
l.replace(cover: CoverA.instantiateFromNib())
l.repeatWhenEnd = true
return l
}()
@IBOutlet weak var playerCollect: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// remove previous download fails file
MMPlayerDownloader.cleanTmpFile()
self.navigationController?.mmPlayerTransition.push.pass(setting: { (_) in
})
offsetObservation = playerCollect.observe(\.contentOffset, options: [.new]) { [weak self] (_, value) in
guard let self = self, self.presentedViewController == nil else {return}
NSObject.cancelPreviousPerformRequests(withTarget: self)
self.perform(#selector(self.startLoading), with: nil, afterDelay: 0.2)
}
playerCollect.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 200, right:0)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in
self?.updateByContentOffset()
self?.startLoading()
}
mmPlayerLayer.getStatusBlock { [weak self] (status) in
switch status {
case .failed(let err):
let alert = UIAlertController(title: "err", message: err.description, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self?.present(alert, animated: true, completion: nil)
case .ready:
print("Ready to Play")
case .playing:
return
// print("Playing")
case .pause:
return
// print("Pause")
case .end:
return
// print("End")
default: break
}
}
mmPlayerLayer.getOrientationChange { (status) in
print("Player OrientationChange \(status)")
}
}
deinit {
offsetObservation?.invalidate()
offsetObservation = nil
print("ViewController deinit")
}
@IBAction func profileButtonTap(_ sender: Any) {
let uid = (Auth.auth().currentUser?.uid)!
let Splash = SpalshScreenViewController()
Splash.GetProfilePicture(uid: uid)
Splash.GetUsername(uid: uid)
Splash.GetName(uid: uid)
Splash.GetClipsNumber(uid: uid)
Splash.GetFollowersNumber(uid: uid)
Splash.GetFollowingsNumber(uid: uid)
performSegue(withIdentifier: "showProfile", sender: nil)
}
}
// This protocol use to pass playerLayer to second UIViewcontroller
extension HomeViewController: MMPlayerFromProtocol {
// when second controller pop or dismiss, this help to put player back to where you want
// original was player last view ex. it will be nil because of this view on reuse view
func backReplaceSuperView(original: UIView?) -> UIView? {
guard let path = self.findCurrentPath(),
let cell = self.findCurrentCell(path: path) as? PlayerCell else {
return original
}
return cell.imgView
}
// add layer to temp view and pass to another controller
var passPlayer: MMPlayerLayer {
return self.mmPlayerLayer
}
func transitionWillStart() {
}
// show cell.image
func transitionCompleted() {
self.updateByContentOffset()
self.startLoading()
}
}
extension HomeViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let m = min(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
return CGSize(width: m, height: m*0.75)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
DispatchQueue.main.async { [unowned self] in
if self.presentedViewController != nil || self.mmPlayerLayer.isShrink == true {
//self.playerCollect.scrollToItem(at: indexPath, at: .centeredVertically, animated: true)
//self.updateDetail(at: indexPath)
} else {
self.presentDetail(at: indexPath)
}
}
}
fileprivate func updateByContentOffset() {
if mmPlayerLayer.isShrink {
return
}
if let path = findCurrentPath(),
self.presentedViewController == nil {
self.updateCell(at: path)
//Demo SubTitle
if path.row == 0, self.mmPlayerLayer.subtitleSetting.subtitleType == nil {
}
}
}
fileprivate func presentDetail(at indexPath: IndexPath) {
self.updateCell(at: indexPath)
mmPlayerLayer.resume()
if let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController {
vc.data = DemoSource.shared.demoData[indexPath.row]
self.present(vc, animated: true, completion: nil)
}
}
fileprivate func updateCell(at indexPath: IndexPath) {
if let cell = playerCollect.cellForItem(at: indexPath) as? PlayerCell, let playURL = cell.data?.play_Url {
// this thumb use when transition start and your video dosent start
mmPlayerLayer.thumbImageView.image = cell.imgView.image
// set video where to play
mmPlayerLayer.playView = cell.imgView
mmPlayerLayer.set(url: playURL)
}
}
@objc fileprivate func startLoading() {
self.updateByContentOffset()
if self.presentedViewController != nil {
return
}
// start loading video
mmPlayerLayer.resume()
}
private func findCurrentPath() -> IndexPath? {
let p = CGPoint(x: playerCollect.contentOffset.x + playerCollect.frame.width/2, y: playerCollect.frame.height/2)
return playerCollect.indexPathForItem(at: p)
}
private func findCurrentCell(path: IndexPath) -> UICollectionViewCell? {
return playerCollect?.cellForItem(at: path)
}
}
extension HomeViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return DemoSource.shared.demoData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PlayerCell", for: indexPath) as? PlayerCell {
cell.data = DemoSource.shared.demoData[indexPath.row]
return cell
}
return UICollectionViewCell()
}
}
更新:
在集合视图上添加高度使其仅显示此处显示的一半:为什么这样做?
您是否为stackview设置了高度限制???@KeshuR。对堆栈视图距离顶部的按钮有10个点,其他三个面都被限制在每一侧(每一侧的左侧和右侧以及距屏幕底部的10个点),因为我希望它占据整个视图。我不明白。显示的图像需要3个堆栈视图。正确的?每个stackview将有一个特色标签和一个集合视图。如果我错了,请纠正我。尝试将stackView的高度限制设置为200左右。删除底部约束。它必须是堆栈视图还是滚动视图?在集合视图的流程为水平的情况下,是否可以让集合视图位于内部的tableview?@KeshuR。移除底部约束显然会使情况变得更糟。我只想有一个标题,然后是集合视图,一个标题,然后是集合视图。等等,您是否为stackview设置了高度限制???@KeshuR。对堆栈视图距离顶部的按钮有10个点,其他三个面都被限制在每一侧(每一侧的左侧和右侧以及距屏幕底部的10个点),因为我希望它占据整个视图。我不明白。显示的图像需要3个堆栈视图。正确的?每个stackview将有一个特色标签和一个集合视图。如果我错了,请纠正我。尝试将stackView的高度限制设置为200左右。删除底部约束。它必须是堆栈视图还是滚动视图?在集合视图的流程为水平的情况下,是否可以让集合视图位于内部的tableview?@KeshuR。移除底部约束显然会使情况变得更糟。我只想有一个标题,然后是集合视图,一个标题,然后是集合视图。等等