From 70bf3e325bcfdbe35219303b81e8eb4bb6347a16 Mon Sep 17 00:00:00 2001 From: knox Date: Wed, 2 May 2018 17:45:35 +0800 Subject: [PATCH] fix layout newChildWidth bug for cocos-creator/engine/issues/2383 --- cocos2d/core/components/CCLayout.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/components/CCLayout.js b/cocos2d/core/components/CCLayout.js index 7315b320d4..be07bcfdb0 100644 --- a/cocos2d/core/components/CCLayout.js +++ b/cocos2d/core/components/CCLayout.js @@ -520,9 +520,17 @@ var Layout = cc.Class({ var maxHeightChildAnchorY = 0; + var activeChildCount = 0; + children.forEach(function(child) { + if (!child.activeInHierarchy) { + return; + } + activeChildCount++; + }); + var newChildWidth = this.cellSize.width; if (this.type !== Type.GRID && this.resizeMode === ResizeMode.CHILDREN) { - newChildWidth = (baseWidth - (this.paddingLeft + this.paddingRight) - (children.length - 1) * this.spacingX) / children.length; + newChildWidth = (baseWidth - (this.paddingLeft + this.paddingRight) - (activeChildCount - 1) * this.spacingX) / activeChildCount; } children.forEach(function(child) { @@ -653,9 +661,17 @@ var Layout = cc.Class({ var containerResizeBoundary = 0; var maxWidthChildAnchorX = 0; + var activeChildCount = 0; + children.forEach(function(child) { + if (!child.activeInHierarchy) { + return; + } + activeChildCount++; + }); + var newChildHeight = this.cellSize.height; if (this.type !== Type.GRID && this.resizeMode === ResizeMode.CHILDREN) { - newChildHeight = (baseHeight - (this.paddingTop + this.paddingBottom) - (children.length - 1) * this.spacingY) / children.length; + newChildHeight = (baseHeight - (this.paddingTop + this.paddingBottom) - (activeChildCount - 1) * this.spacingY) / activeChildCount; } children.forEach(function(child) {