2016-07-06 6 views
2

Ich bin super neue iOS-Entwicklung und StackViews im Allgemeinen und brauche Hilfe Berechnung der dynamischen Höhe in Fällen, in denen eine Stapelansicht nicht angezeigt wird. Es gibt Fälle, in denen bestimmte Elemente nicht gezeigt werden, abhängig davon, was ich vom Server zurückbekomme.Wie kann ich die Höhe einer UIView dynamisch anpassen, wenn eine Mitgliedsstapelansicht nicht mehr angezeigt wird?

Wenn ich jedoch removeArrangedSubview aufrufen, wird das Element entfernt, aber die Höhe wird nicht dynamisch angepasst. Wie kann ich das beheben?

Ich möchte den Interface Builder alle zusammen vermeiden und dies nur programmgesteuert tun. Ich habe Layout-Anker für Einschränkungen verwendet.

Hier ist mein Code. Sie können einen Spielplatz aufstellen, um es zu sehen.

//: 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) 

Vor Element entfernt: enter image description here

Nach Element entfernt wird: enter image description here

Ich möchte diese Lücke dynamisch angepasst gegangen und die Höhe werden.

Antwort

0

Sie können eine Einschränkung für die Höhe der Ansicht hinzufügen, wenn Sie das automatische Layout verwenden.

So wie für die Ersteinrichtung, Sie so etwas wie tun könnten:

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

Vielen Dank für Ihre Antwort, aber ich bin nicht mit automatischem Layout. Ich habe deinen Vorschlag ausprobiert, aber es ist nicht leicht zu der Spielfelddatei zu transportieren, die ich habe, das ist genau der Code, den ich eingefügt habe. – Aetherynne

Verwandte Themen