2016-07-06 11 views
2

私は新しくiOSの開発とStackViewを新しくしており、スタックビューが表示されない場合に動的な高さを計算するのに役立つ必要があります。いくつかの要素がサーバーから返ってくるものに応じて表示されない場合があります。メンバースタックビューが表示されなくなったときにUIViewの高さを動的に調整するにはどうすればよいですか?

しかし、私がremoveArrangedSubviewを呼び出すと、要素は削除されますが、高さは動的に調整されません。これをどうすれば解決できますか?

私はInterface Builderを一緒に避け、プログラムでこれを行うだけです。私は制約のためにレイアウトアンカーを使用しています。

ここに私のコードです。あなたはそれを見るために遊び場に置くことができます。要素の前

//: Playground - noun: a place where people can play 


import UIKit 
import Foundation 

let view = UIView(frame: CGRect(x: 0, y: 0, width: 800, height: 140)) 

let firstStackView = UIStackView(frame: CGRectZero) 
//firstStackView.heightAnchor.constraintGreaterThanOrEqualToConstant(40).active = true 
firstStackView.axis = .Vertical 
firstStackView.alignment = .Fill 
firstStackView.distribution = .EqualSpacing 

let titleStackView = UIStackView(frame: CGRectZero) 
titleStackView.axis = .Horizontal 
titleStackView.alignment = .Fill 
titleStackView.distribution = .Fill 
titleStackView.spacing = 3 
firstStackView.addArrangedSubview(titleStackView) 

let productStackView = UIStackView(frame: .zero) 
productStackView.axis = .Horizontal 
productStackView.alignment = .Leading 
productStackView.distribution = .Fill 
productStackView.spacing = 3 
firstStackView.addArrangedSubview(productStackView) 
//firstStackView.removeArrangedSubview(productStackView) 

let secondStackView = UIStackView(frame: CGRectZero) 
//secondStackView.heightAnchor.constraintEqualToConstant(30).active = true 
secondStackView.axis = .Horizontal 
secondStackView.distribution = .EqualSpacing 


let title = UILabel(frame: CGRectZero) 
title.text = "test1" 
title.textColor = .blackColor() 
//labelOne.backgroundColor = .blueColor() 
let size = title.sizeThatFits(CGSizeZero) 
print("\(size)") 
title.widthAnchor.constraintEqualToConstant(size.width).active = true 
//labelOne.heightAnchor.constraintEqualToConstant(30).active = true 
titleStackView.addArrangedSubview(title) 


let assigneeLabel = UILabel(frame: CGRectZero) 
assigneeLabel.text = "test2" 
assigneeLabel.textColor = .blackColor() 
//labelTest.backgroundColor = .redColor() 
assigneeLabel.textAlignment = .Left 
//labelTest.heightAnchor.constraintEqualToConstant(30).active = true 
titleStackView.addArrangedSubview(assigneeLabel) 

let actions = UIButton(type: .Custom) 
//buttonOne.backgroundColor = .redColor() 
actions.setTitle("some button", forState: .Normal) 
actions.setTitleColor(.blackColor(), forState: .Normal) 
titleStackView.addArrangedSubview(actions) 


let productOne = UILabel(frame: CGRectZero) 
productOne.text = "something1" 
productOne.numberOfLines = 0 
let productLabelSize = productOne.sizeThatFits(CGSizeZero) 
productOne.widthAnchor.constraintEqualToConstant(productLabelSize.width).active = true 
productOne.textColor = .blackColor() 
//labelTwo.backgroundColor = .blueColor() 
productStackView.removeArrangedSubview(productOne) 
//productStackView.addArrangedSubview(productOne) 


let productTwo = UILabel(frame: CGRectZero) 
productTwo.text = "something2" 
productTwo.numberOfLines = 0 
//productTwo.heightAnchor.constraintEqualToConstant(30).active = true 
productTwo.textColor = .blackColor() 
//labelTwo.backgroundColor = .blueColor() 
productStackView.removeArrangedSubview(productTwo) 
//productStackView.addArrangedSubview(productTwo) 


let labelThree = UILabel(frame: CGRectZero) 
labelThree.text = "sometime" 
//labelThree.heightAnchor.constraintEqualToConstant(30).active = true 
labelThree.textColor = .blackColor() 
//labelThree.backgroundColor = .blueColor() 
firstStackView.addArrangedSubview(labelThree) 


let descriptionView = UILabel(frame: CGRectZero) 
descriptionView.text = "some description about something" 
descriptionView.textColor = .blackColor() 
//descriptionView.backgroundColor = .redColor() 
secondStackView.addArrangedSubview(descriptionView) 

let tagsView = UILabel(frame: CGRectZero) 
tagsView.text = "some more things" 
tagsView.textColor = .blackColor() 
secondStackView.addArrangedSubview(tagsView) 
secondStackView.trailingAnchor.constraintEqualToAnchor(tagsView.trailingAnchor).active = true 


let stackView = UIStackView(arrangedSubviews: [firstStackView, secondStackView]) 
stackView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20) 
stackView.layoutMarginsRelativeArrangement = true 
stackView.axis = .Vertical 
stackView.frame = view.bounds 
stackView.distribution = .FillProportionally 

view.addSubview(stackView) 

除去さ:enter image description here

要素が除去された後:enter image description here

私はギャップがなくなっすると高さが動的に調整たいです。

答えて

0

自動レイアウトを使用している場合は、ビューの高さに制約を追加できます。だから、初期セットアップのためのように、あなたのような何かを行うことができ

class YourClass : UIViewController() { 

    var heightConstraint = NSLayoutConstraint() 

    func someMethod() { 

    // load your View 
    // get the height of view. 
    heightConstraint = yourView.heightAnchor.constraintEqualToConstant(height) 
    self.view.addConstraint(heightConstraint) 

    } 

    func deleteMemberStackView() { 

    /// After deleting the member, get the new height of the view and do this 
    self.view.removeConstraint(heightConstraint) 
heightConstraint = yourView.heightAnchor.constraintEqualToConstant(height) 
    self.view.addConstraint(heightConstraint) 

    UIView.animateViewDuration(0.3, completion: { 
      self.view.layoutIfNeeded() 
    }) 
    } 

} 
+0

はご回答いただきありがとうございますが、私は、自動レイアウトを使用していないのです。私はあなたの提案を試みましたが、私が行っているプレイグラウンドファイルには簡単に移植できません。これは貼り付けたコードとまったく同じです。 – Aetherynne

関連する問題