2014-09-02 6 views
6

Gibt es ein iOS-Äquivalent zu Android View.GONE?iOS entspricht View.GONE

In Android wird das Einstellen einer Ansicht auf GONE unsichtbar gemacht und gewährleistet, dass die Ansicht keinen Platz im Layout einnimmt. Ich weiß mit iOS, können Sie einen Blick auf versteckte mit

[viewName setHidden:true]; 

gesetzt, aber die Aussicht nimmt noch Platz im Layout, und ich denke, dass es ineffizient wäre, um vollständig die Ansicht zu entfernen und sie später neu erstellen.

(Hinweis: Ich habe diesen Post gesehen: iOS equivalent for Android View.GONE visibility mode, aber es gibt keine akzeptierte Antwort, und die Höhe auf 0 hat nicht funktioniert, da die nachfolgenden Ansichten auf der Seite nicht verschoben haben, nachdem meine Ansicht entfernt wurde)

+0

Dieser Beitrag gibt Ihnen vielleicht eine Idee. [LinkIsHere] (http://stackoverflow.com/questions/17869268/ios-äquivalenz-für-android-view-gone-visibility-mode) – TeachMeJava

Antwort

4

Nur Äquivalent kann AFAIK sein:

[yourView removeFromSuperview] 

Bis Sie view von seinem superview in ios entfernen sie Platz im Layout nehmen.

Je nach Bedarf können Sie Ansicht hinzufügen oder entfernen, wenn erforderlich (wie view.GONE in Android).

+0

danke für Ihre Antwort. Ich habe versucht, dies hinzuzufügen, aber es schien, als ob die Ansichten unter dem removedView nicht hochgeschoben wurden. Ist das etwas anderes, das ich mit einschließen muss? – scientiffic

+0

oky, um die Ansichten nach oben zu verschieben, müssen Sie sie manuell verschieben oder Sie können die 'Auto Layout'-Funktion verwenden. Sobald Sie Ihre Ansicht entfernen; Ändern Sie die Position von "Ansicht" darunter nach oben, gleich der Höhe der entfernten Ansicht. Überprüfen Sie diese Verbindung, um Ansicht zu verschieben: 'http: // stackoverflow.com/questions/5161096/simple-way-zu-ändern-die-Position-von-uiview' – astuter

+0

https://stackoverflow.com/questions/45454992 –

2

Ich mache gerade den Übergang zwischen Android und iOS mit Swift und das war eines meiner ersten Probleme. Nach der Suche im Internet habe ich festgestellt, dass einige Leute gute Ergebnisse erzielt haben, indem Sie die Höhe oder Breite der UIView auf 0 gesetzt haben, je nachdem, ob die Ansicht vertikal oder horizontal verschwinden soll. Um diese Idee in meinem App zu implementieren I definiert zwei Funktionen wie folgt:

enum Direction { 
    case HORIZONTAL, VERTICAL 
} 

func removeView(view: UIView, direction: Direction) { 
    // Removing the view vertically 
    if direction == .VERTICAL { 
     let constraint = NSLayoutConstraint(item: view as UIView, 
              attribute: NSLayoutAttribute.Height, 
              relatedBy: .Equal, 
              toItem: nil, 
              attribute: NSLayoutAttribute.NotAnAttribute, 
              multiplier: 0, 
              constant: 0) 
     view.addConstraint(constraint) 
    } else { // Removing the view horizontally 
     let constraint = NSLayoutConstraint(item: view as UIView, 
              attribute: NSLayoutAttribute.Width, 
              relatedBy: .Equal, 
              toItem: nil, 
              attribute: NSLayoutAttribute.NotAnAttribute, 
              multiplier: 0, 
              constant: 0) 
     view.addConstraint(constraint) 
    } 
} 

// Removing the view both horizontally and vertically 
func removeView(view: UIView) { 
    let constraintH = NSLayoutConstraint(item: view as UIView, 
              attribute: NSLayoutAttribute.Height, 
              relatedBy: .Equal, 
              toItem: nil, 
              attribute: NSLayoutAttribute.NotAnAttribute, 
              multiplier: 0, 
              constant: 0) 
    let constraintW = NSLayoutConstraint(item: view as UIView, 
              attribute: NSLayoutAttribute.Width, 
              relatedBy: .Equal, 
              toItem: nil, 
              attribute: NSLayoutAttribute.NotAnAttribute, 
              multiplier: 0, 
              constant: 0) 
    view.addConstraint(constraintH) 
    view.addConstraint(constraintW) 
} 

Und es scheint, auf dem Simulator zu arbeiten. Hoffe das hilft.

+0

https ://Paketüberfluss.com/questions/45454992 –

5

Fügen Sie die Breite/Höhe Einschränkungen mit constant = 0-your View wird your View machen Breite und Höhe = 0 (wie es GONE)

// set the height constraint to 0 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:theGoneView 
                    attribute:NSLayoutAttributeHeight 
                    relatedBy:NSLayoutRelationEqual 
                     toItem:nil 
                    attribute:NSLayoutAttributeNotAnAttribute 
                    multiplier:1.0 
                    constant:0]]; 

// set the width constraint to 0    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:theGoneView 
                   attribute:NSLayoutAttributeWidth 
                   relatedBy:NSLayoutRelationEqual 
                    toItem:nil 
                   attribute:NSLayoutAttributeNotAnAttribute 
                   multiplier:1.0 
                   constant:0]]; 

In Swift

// set the width constraint to 0 
let widthConstraint = NSLayoutConstraint(item: theGoneView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 0) 
view.addConstraint(widthConstraint) 

// set the height constraint to 0   
let heightConstraint = NSLayoutConstraint(item: theGoneView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 0) 
    view.addConstraint(heightConstraint) 

Oder diese UIView Erweiterung

extension UIView {   

    func goAway() { 
     // set the width constraint to 0 
     let widthConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 0) 
     superview!.addConstraint(widthConstraint) 

     // set the height constraint to 0 
     let heightConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 0) 
     superview!.addConstraint(heightConstraint) 
    } 

} 

Hope diese Hilfe

+1

Große Antwort !!! BTW "theGoneView" ist der Name der Ansicht, die man machen möchte. – Fay007

+0

@Phan Van Linh Ich habe den gleichen Code verwendet, aber es funktioniert nicht ... https: //stackoverflow.com/questions/45454992/remove-white-space-after-hide-views-in-scrollview –

1

Die sauberste Lösung für mich ist, die Komponenten, die Sie bewegen möchten in eine StackView (iOS 9.0+), dann durch Aufruf UIView.isHidden = true auf die gewünschte Ansicht, Sie genau die Ansicht zu erreichen. GONE-Effekt, weil Inhalt umschließt.