Swift:添加子视图时视图的框架发生变化
Posted
技术标签:
【中文标题】Swift:添加子视图时视图的框架发生变化【英文标题】:Swift : View's frame getting altered when a subview is added 【发布时间】:2016-03-23 01:39:45 【问题描述】:我在一个向上滑动的视图中有一个集合视图和两个容器视图(我们称之为 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】:我通过从项目中删除 AutoLayout 并求助于在代码中设置布局解决了这个问题。
我仍然想知道为什么会这样,因为这有助于我更好地理解 AutoLayout。
【讨论】:
以上是关于Swift:添加子视图时视图的框架发生变化的主要内容,如果未能解决你的问题,请参考以下文章