Redimensionner UIView personnalisé à partir de XIB

J'ai le problème suivant: j'ai créé une coutume UIView classe et de sa mise en page dans XIB fichier. Disons que la taille de ma vue personnalisée dans XIB est de 150 x 50. Je l'ai activé sizeClasses (wAny hAny) et AutoLayout. Dans le cadre de simulations Métriques j'ai mis Size = FreeformStatus Bar = Nonetous les other = Inferred.

Code de mon custom UIView classe ressemble à ceci:

#import "CustomView.h"

@implementation CustomView

#pragma mark - Object lifecycle

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    if (self) {
        [self setup];
    }

    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];

    if (self) {
        [self setup];
    }

    return self;
}

#pragma mark - Private & helper methods

- (void)setup {
    if (self.subviews.count == 0) {
        [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
        self.bounds = self.view.bounds;
        [self addSubview:self.view];
    }
}

@end

Dans mon UIViewController où je veux ajouter cette coutume UIViewj'ai fait factice UIView (dont la taille j'ai mis dans Interface Builder) lorsque je veux ajouter mon CustomView et je voudrais que mon CustomView adapte les limites de mon conteneur affichage.

Je suis en train de le faire comme ceci:

_vCustomView = [[CustomView alloc] init];
_vCustomView.translatesAutoresizingMaskIntoConstraints = NO;
[_vContainer addSubview:_vCustomView];

[_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
[_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
[_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];

Mais pas de chance. Disons que mon conteneur affichage est de 300 x 100, lorsque j'ajoute à mon habitude UIViewma vue personnalisée est encore 150 x 50.

J'ai essayé de travailler sans conteneur view - pour ajouter à mon habitude UIView objet directement à l'auto.vue de ma UIViewController et de définir la largeur et la hauteur avec:

  • autoLayout contraintes
  • en utilisant initWithFrame lors de l'initialisation de mon custom UIView

mais encore une fois - pas de chance. Affichage personnalisé est toujours 150 x 50.

Quelqu'un peut m'expliquer comment puis-je par programme ajouter à mon habitude UIView fait dans XIB et le redimensionner à l'aide autoLayout contraintes?

Merci beaucoup à l'avance.

Edit #1: l'Erreur que je reçois lorsque vous essayez d'exécuter Andrea code sur mon CustomView

Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x17489b760 V:[UIView:0x17418d820(91)]>",
    "<NSLayoutConstraint:0x170c9bf80 V:|-(0)-[CustomView:0x1741da7c0]   (Names: '|':CustomView:0x1741da8b0 )>",
    "<NSLayoutConstraint:0x170c9bfd0 V:[CustomView:0x1741da7c0]-(0)-|   (Names: '|':CustomView:0x1741da8b0 )>",
    "<NSLayoutConstraint:0x170c9be90 V:|-(0)-[CustomView:0x1741da8b0]   (Names: '|':UIView:0x17418d820 )>",
    "<NSLayoutConstraint:0x170c9bee0 CustomView:0x1741da8b0.bottom == UIView:0x17418d820.bottom>",
    "<NSAutoresizingMaskLayoutConstraint:0x170c9dba0 h=--& v=--& CustomView:0x1741da7c0.midY == + 25.0>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170c9bfd0 V:[CustomView:0x1741da7c0]-(0)-|   (Names: '|':CustomView:0x1741da8b0 )>

Edit #2: Ça marche!!!

Après @Andrea ajouté:

view.translatesAutoresizingMaskIntoConstraints = NO;

dans son:

- (void) stretchToSuperView:(UIView*) view;

méthode, tout fonctionne comme je m'y attendais.

Grâce à @Andrea.

source d'informationauteur uerceg