IOS AutoLayout Formato visivo - Allineamento
-
12-12-2019 - |
Domanda
In IOS6, sto usando Autolayouts.
- .
- Ho 2 visualizzazioni V1 e V2 creati programmaticamente.
- v2 viene aggiunto come sottoview a v1
- I vincoli per V1 sono già stati creati programmaticamente (non mostrati qui).
- Voglio che V1 e V2 avessero delle stesse dimensioni e voglio che V2 venga posizionato sopra V1 esattamente il centro orizzontale e verticale corrispondente in modo tale che V2 sia esattamente in cima a V1
Domanda:
- .
- Come posso ottenere questo (preferibilmente usare il formato visivo, se non è possibile, puoi suggerire approccio alternativo)?
Codice - Dato Di seguito è il mio tentativo ma tuttavia non fornisce l'output desiderato.- Nessun errore lanciato.V1 è posizionato correttamente ma V2 è uno dell'angolo a sinistra del V1
Nota - Ho spiegato di seguito nei commenti, cosa sto cercando di fare.
[v1 addSubView:v2];
v1.translatesAutoresizingMaskIntoConstraints = NO;
v2.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(v1,
v2);
//I wanted v1 and v2 to be of the same height and wanted the center Y to be aligned together
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[v2(==v1)]"
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:viewDictionary];
//I wanted v1 and v2 to be of the same height and wanted the center X to be aligned together
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[v2(==v1)]"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:viewDictionary];
[v1 addConstraints:horizontalConstraints];
[v1 addConstraints:verticalConstraints];
. Soluzione
non dovrebbe solo funzionare?
NSMutableArray *constraints = [NSMutableArray new];
constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|[v2]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(v2)];
constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v2]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(v2)];
[v1 addConstraints:constraints];
.
Cosa fa è rendere V2 che allunga la sua altezza e la larghezza alle dimensioni della sua supervisione, che in questo caso è V1 e posizionarlo così è proprio sulla parte superiore della sua supervisione.
Sembra che nel tuo caso, non è necessario allineare la vista Centerx e il corso.Altrimenti, non sto capire il contesto della tua domanda correttamente.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow