2014-09-02 23 views
6

Esiste un equivalente iOS per Android View.GONE?iOS equivalente a View.GONE

In Android, l'impostazione di una vista su GONE la rende invisibile e garantisce che la vista non occupi spazio nel layout. So che con iOS, è possibile impostare in vista di nascosto con

[viewName setHidden:true]; 

ma la vista prende ancora spazio nel layout, e penso che sarebbe inefficiente per rimuovere completamente la vista e ricrearlo in seguito.

(nota: Ho visto questo post: iOS equivalent for Android View.GONE visibility mode ma non c'è risposta accettata, e impostare l'altezza a 0 non ha funzionato per me, come i punti di vista successivi riguardanti la pagina non si mosse dopo il mio punto di vista è stato rimosso) Solo

+0

Quel post potrebbe darvi un'idea. [LinkIsHere] (http://stackoverflow.com/questions/17869268/ios-equivalent-for-android-view-gone-visibility-mode) – TeachMeJava

risposta

4

possibile equivalente può essere per quanto ne so:

[yourView removeFromSuperview] 

Finché si rimuove view dalla sua superview in ios ci vorrà spazio nel layout.

Quindi, in base alle proprie esigenze, è possibile aggiungere o rimuovere la vista quando richiesto (lo stesso di view.GONE in Android).

+0

grazie per la tua risposta. Ho provato ad aggiungere questo, ma sembrava che le viste sotto removeView non fossero state spostate. È qualcos'altro che devo includere? – scientiffic

+0

oky, per spostare le viste in alto è necessario spostarle manualmente oppure provare a utilizzare la funzione 'Layout automatico'. Una volta rimossa la vista; Cambia la posizione di 'view' sotto di essa al rialzo uguale all'altezza della vista rimossa. Controlla questo link per spostare la vista: 'http: // stackoverflow.com/questions/5161096/simple-way-to-change-the-position-of-uiview' – astuter

+0

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

2

Attualmente sto facendo la transizione tra Android e iOS utilizzando Swift e quello è stato uno dei miei primi problemi. Dopo aver cercato su Internet, ho scoperto che alcune persone hanno ottenuto buoni risultati impostando l'altezza o la larghezza di UIView su 0, a seconda che si desideri che la vista scompaia verticalmente o orizzontalmente. Per implementare questa idea nella mia app ho definito due funzioni in questo modo:

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) 
} 

E sembra funzionare sul simulatore. Spero che questo ti aiuti.

+0

https : // StackOverflow.it/questions/45454992 –

5

Aggiungere i vincoli di larghezza/altezza con constant = 0-your View farà your View hanno larghezza e altezza = 0 (come è 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) 

O questa estensione UIView

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) 
    } 

} 

Spero che questo aiuto

+1

Ottima risposta !!! btw the "theGoneView" è il nome della vista che si vuole eliminare. – Fay007

+0

@Phan Van Linh ho usato lo stesso codice ma non funziona ... https: //stackoverflow.com/questions/45454992/remove-white-space-after-hide-views-in-scrollview –

1

La soluzione più pulita per me è incorporare i componenti che si desidera "spostare" in un StackView (iOS 9.0+), quindi, chiamando UIView.isHidden = true nella vista desiderata, si ottiene esattamente la vista. Effetto GONE perché StackView avvolge il contenuto.