Ios 在swift中按下集合视图单元格时如何修复自定义视图中的约束

Ios 在swift中按下集合视图单元格时如何修复自定义视图中的约束,ios,swift,uiview,uicollectionview,orientation,Ios,Swift,Uiview,Uicollectionview,Orientation,我有一个测试应用程序,我第一次尝试100%以编程方式创建它。我有一个简单的collectionView,每个单元格中都有一个图像和一个标签 当我按下单元格时,会弹出一个自定义视图,其中有两个视图,红色和蓝色,将来会有内容,但现在只有两个视图 我想让这个应用程序在纵向和横向两种方向上都工作,一开始它工作得很好。我第一次按下单元格时,弹出视图在纵向和横向两个方向上都起作用。 这是我希望在按下单元格时发生的情况,但当我单击视图外使其消失,然后按下任何单元格时,问题就会出现: 第一个方向,无论是

我有一个测试应用程序,我第一次尝试100%以编程方式创建它。我有一个简单的collectionView,每个单元格中都有一个图像和一个标签

当我按下单元格时,会弹出一个自定义视图,其中有两个视图,红色和蓝色,将来会有内容,但现在只有两个视图

我想让这个应用程序在纵向和横向两种方向上都工作,一开始它工作得很好。我第一次按下单元格时,弹出视图在纵向和横向两个方向上都起作用。

这是我希望在按下单元格时发生的情况,但当我单击视图外使其消失,然后按下任何单元格时,问题就会出现:

第一个方向,无论是横向还是纵向,都很好,但是当我现在改变方向时,问题出现了,因为第二个或更多的单元格被按下

我尝试使用首次登场视图层次结构,这就是错误屏幕的样子

我遵循的主要思想是本网站的教程:

我能够完成本教程,并且效果很好。我的不同之处在于,我的视图假设在用户需要时出现或消失

这是具有collectionView的视图控制器的代码:

import UIKit

extension ViewController: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 100, height: 150)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
}


extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TestingCollectionViewCell
        cell.backgroundColor = .white
        let img = UIImage(named: self.items[indexPath.row])
        cell.imageView.image = img
        cell.imageName.text = "\(self.items[indexPath.row])"
        return cell
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("\(items[indexPath.row])")
        self.presentInfoView(withInfo: items[indexPath.row])
    }
}

class ViewController: UIViewController, BlurDelegate {

    func removeBlurView() {
        for subview in view.subviews {
            if subview.isKind(of: UIVisualEffectView.self) {
                subview.removeFromSuperview()
                self.infoView.removeFromSuperview()
            }
        }
    }

    let infoView: InfoView = {
       let view = InfoView()
        view.layer.cornerRadius = 5
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    fileprivate var items: [String] = [
        "photo1",
        "photo2",
        "photo3",
        "photo4",
        "photo5",
        "photo6",
        "photo7",
        "photo8",
        "photo9",
        "photo10",
        "photo11",
        "photo12",
        "photo13",
        "photo14",
        "photo15",
        "photo16",
        "photo17",
        "photo18",
        "photo19",
        "photo20",
        "photo21",
        "photo22",
        "photo23",
        "photo24"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        let collection = UICollectionView(frame: view.frame, collectionViewLayout: UICollectionViewFlowLayout())
                //allows us to use auto layout constraints
        collection.translatesAutoresizingMaskIntoConstraints = false
        collection.backgroundColor = .black
        view.addSubview(collection)
        collection.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        collection.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        collection.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        collection.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

        collection.dataSource = self
        collection.delegate = self

        collection.register(TestingCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        self.navigationItem.title = "Testing"

    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        var touch: UITouch? = touches.first
        if touch?.view != infoView {
            dismissView()

        }
    }


    func dismissView() {

        //dismiss(animated: true, completion: nil)
        removeBlurView()

    }

    func setBlurView() {
        let blurView = UIVisualEffectView()
        blurView.frame = view.frame
        blurView.effect = UIBlurEffect(style: .regular)
        blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(blurView)
    }

    func presentInfoView(withInfo info: String) {
        setBlurView()
        view.addSubview(infoView)
        infoView.addView()
        let img = UIImage(named: info)
        infoView.imageView.image = img
        infoView.nameLbl.text = info


        infoView.backgroundColor = .white
        infoView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        infoView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.8).isActive = true
        //infoView.widthAnchor.constraint(equalToConstant: view.frame.width - 64).isActive = true
        infoView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.8).isActive = true
        //infoView.heightAnchor.constraint(equalToConstant: view.frame.height - 64).isActive = true
        infoView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -44).isActive = true
        infoView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)

        infoView.alpha = 0

        UIView.animate(withDuration: 0.5) {
            self.infoView.alpha = 1
            self.infoView.transform = .identity
        }
    }

}
我从上面添加的站点中得到了这个观点。我不得不改变一些东西,因为它是一个视图,但它应该工作相同。我认为错误在于此,但我尝试更改纵向和横向的约束,但没有成功。我试着把它写下来,看看哪些约束被删除了,然后又被更新了,但我就是找不到哪一个正困扰着我。 我知道问题应该出现在红色视图的顶部锚点上,或者蓝色视图的底部锚点上,但我无法确定哪一个是错误的

我希望这是足够的信息,但请问我是否还有什么我可以帮助的

另外,如果不太麻烦的话,如果您注意到视图出现时,导航栏似乎没有模糊。这使自定义视图看起来像是从顶部截断的。如果您知道模糊导航栏的方法,或者尝试向下推视图的顶部锚点,使其看起来不那么糟糕,将不胜感激

我试着用

self.navigationItem.bottomAnchor
我试图在视图中添加一个带有常数的顶锚,但是我上面的配置是我能找到的最好的方法,使视图每次都正确完美地显示出来。
非常感谢

presentInfoView函数删除infoView.addView()

self.navigationItem.bottomAnchor
     override init(frame: CGRect) {
                super.init(frame: frame)
                NotificationCenter.default.addObserver(self, selector: #selector(toggleConstraints), name: UIDevice.orientationDidChangeNotification, object: nil)
                addView()
}