Swift:添加子视图时,View的框架会发生变化

时间:2016-03-23 01:39:46

标签: ios swift uiview uiviewcontroller uigesturerecognizer

我在视图中有一个集合视图和两个容器视图,它们向上滑动(让我们称之为menugrid)。当menugrid滑动时,框架被设置成覆盖整个屏幕。我正在尝试构建一个功能,用户可以将UICollectionView单元拖放到容器视图中。当我开始拖放时,menugrid视图返回到屏幕底部的初始位置。

以下是一些代码:

func handlePanForCell(gestureRecognizer: UILongPressGestureRecognizer) {

        let pressPoint: CGPoint =  gestureRecognizer.locationInView(gestureRecognizer.view)
        let localPoint : CGPoint = self.menuGridView.convertPoint(pressPoint, fromView: gestureRecognizer.view!)
        var cell : MenugridCollectionViewCell  = MenugridCollectionViewCell()
        if(gestureRecognizer.view is UICollectionViewCell){
            if (gestureRecognizer.state == UIGestureRecognizerState.Began) {
                cell  =  gestureRecognizer.view as! MenugridCollectionViewCell
                UIGraphicsBeginImageContext(cell.bounds.size);
                cell.layer.renderInContext(UIGraphicsGetCurrentContext()!)
                let cellImage : UIImage = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                self.movingCell =  UIImageView.init(image: cellImage)
                self.movingCell?.alpha = 0.75
                //let cellFrameInSuperview : CGRect =  self.menuGridCollectionView.convertRect(cell.frame, toView: self.menuGridView)
                self.movingCell?.center =  localPoint
                //self.movingCell?.frame = cellFrameInSuperview
                self.view.addSubview(self.movingCell!)
            }
            if (gestureRecognizer.state == UIGestureRecognizerState.Changed) {
                self.movingCell?.center = localPoint
                setMenuGridViewOnTop()
            }
            if (gestureRecognizer.state == UIGestureRecognizerState.Ended) {

                // Check to see if the drop area is in the personalized info-area
                let isContaining =  self.menuGridView.convertRect(self.firstContainerView.frame, toView: self.menuGridView).contains(self.movingCell!.frame) || self.middleView.convertRect(self.secondInfoView.frame, toView: self.menuGridView).contains(self.movingCell!.frame)
                if isContaining
                {
                    if(self.middleView.convertRect(self.firstInfoView.frame, toView: self.view).contains(self.movingCell!.frame)) {
                        //let index = self.findIndexOfViewController(self.infoViewArray[0])
                        //let oldViewController : UIViewController = self.childViewControllers[Int(index)] as UIViewController
                        self.switchInfoViews(nil, toView: (gestureRecognizer.view as! MenugridCollectionViewCell).name!, atPosition: 0, parentView: "menuGridView")
                    }
                    else {
                        self.switchInfoViews(nil, toView: (gestureRecognizer.view as! MenugridCollectionViewCell).name!, atPosition: 1, parentView: "menuGridView")
                    }
                    //self.movingCell?.removeFromSuperview()
                }
                self.movingCell?.removeFromSuperview()
                self.movingCell = nil
                //setMenuGridViewOnTop()

                //self.view.viewWithTag(1000)?.removeFromSuperview()
                //self.view.viewWithTag(1001)?.removeFromSuperview()
                //self.middleView.alpha = 1
                //self.childViewControllers[0].view.alpha = 1
                //self.childViewControllers[1].view.alpha = 1
                //self.topView.alpha = 1
            }
        }
        //setMenuGridViewOnTop()
    }



//MARK: -  Switch Views
    func switchInfoViews(fromViewController : UIViewController?, toView : String, atPosition : CGFloat, parentView : String) {

        var newViewController : InfoViewController = InfoViewController()
        var storyBoardIdOfNewView =  String()
        if(toView == "Doctor"){
            newViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ProviderSearchInfoViewControllerID") as! ProviderSearchInfoViewController
            storyBoardIdOfNewView = "ProviderSearchInfoViewControllerID"
        }
        else if(toView == "PayBill"){
            newViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("PayBillInfoViewControllerID") as! PayBillInfoViewController
            storyBoardIdOfNewView = "PayBillInfoViewControllerID"
        }
        else if(toView == "Symptoms"){
            newViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SymptomsInfoViewControllerID") as! SymptomsInfoViewController
            storyBoardIdOfNewView = "SymptomsInfoViewControllerID"
        }
        else if(toView == "Handouts"){
            newViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("PatientHandoutInfoViewControllerID") as! PatientHandoutInfoViewController
            storyBoardIdOfNewView = "PatientHandoutInfoViewControllerID"
        }
        else if(toView == "FirstAid"){
            newViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("FirstAidInfoViewControllerID") as! FirstAidInfoViewController
            storyBoardIdOfNewView = "FirstAidInfoViewControllerID"
        }

        if(parentView == "menuGridView") {

                let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier(storyBoardIdOfNewView)
                if(atPosition == 0){
                    self.firstContainerView.subviews.forEach({ $0.removeFromSuperview() })
                    viewController.view.frame = CGRectMake(0, 0, (self.menuGridView.frame.width / 2) - 3, self.menuGridView.frame.height * 0.3)//self.firstContainerView.frame
                    self.firstContainerView.addSubview(viewController.view)
                }
                else {
                    self.secondContainerView.subviews.forEach({ $0.removeFromSuperview() })
                    viewController.view.frame = CGRectMake(0, 0, (self.menuGridView.frame.width / 2) - 3, self.menuGridView.frame.height * 0.3)//self.secondContainerView.frame
                    self.secondContainerView.addSubview(viewController.view)
                }
            if(self.menuAtTop){
                self.setMenuGridViewOnTop()
            }
            }
            else if(parentView == "middleView"){

                if(!self.checkIfViewControllerIsAlreadyDisplayed(newViewController))
                {
                    fromViewController?.willMoveToParentViewController(nil)
                    fromViewController?.view.removeFromSuperview()
                    fromViewController?.removeFromParentViewController()
                    self.movingCell?.removeFromSuperview()
                    newViewController.view.frame = CGRectMake(0 + atPosition * self.middleView.frame.size.width / 2, 0 , self.middleView.frame.size.width / 2, self.middleView.frame.size.height)
                    self.middleView.addSubview(newViewController.view)
                    self.addChildViewController(newViewController)
                    newViewController.didMoveToParentViewController(self)
                    if(self.infoViewArray.count > 0) {
                        self.infoViewArray.removeAtIndex(Int(atPosition))
                        self.infoViewArray.insert(toView, atIndex: Int(atPosition))
                    }
                }
            }
    }

可能导致什么?我很难过为什么会发生这种情况,并希望得到任何帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

我通过从项目中删除AutoLayout并在代码中设置布局来解决此问题。

我仍然想知道为什么会这样,这有助于我更好地理解AutoLayout。