¿Por qué -frameForAlignmentRect: and -alignmentRectForFrame: no invoca IOS

Quiero usar -frameForAlignmentRect: y -alignmentRectForFrame: para especificar los rectos de alignment en mis aplicaciones de iOS. Pero estos methods no llaman.

Tengo un código casi idéntico para iOS y Mac. Funciona perfectamente en OS X pero no en iOS.

Versión de Mac

 @implementation CustomView #define SHADOW_SIZE 10 - (void)drawRect:(NSRect)dirtyRect { NSBezierPath *path; [[NSColor networkingColor] set]; path = [NSBezierPath bezierPathWithRect:dirtyRect]; [path fill]; // Draw a shadow NSRect rect = NSMakeRect(SHADOW_SIZE, 0, dirtyRect.size.width - SHADOW_SIZE, dirtyRect.size.height - SHADOW_SIZE); path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:32 yRadius:32]; [[[NSColor blackColor] colorWithAlphaComponent:0.3f] set]; [path fill]; // Draw shape rect.origin = CGPointMake(0, SHADOW_SIZE); path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:32 yRadius:32]; [[NSColor orangeColor] set]; [path fill]; } #define USE_ALIGNMENT_RECTS 1 #if USE_ALIGNMENT_RECTS - (NSRect)frameForAlignmentRect:(NSRect)alignmentRect { NSRect rect = alignmentRect; rect.origin.y -= SHADOW_SIZE; rect.size.width += SHADOW_SIZE; rect.size.height += SHADOW_SIZE; return rect; } - (NSRect)alignmentRectForFrame:(NSRect)frame { NSRect rect; rect.origin = CGPointMake(frame.origin.x, frame.origin.y + SHADOW_SIZE); rect.size.width = frame.size.width - SHADOW_SIZE; rect.size.height = frame.size.height - SHADOW_SIZE; return rect; } #endif @end @implementation AppDelegate - (void)awakeFromNib { _view = _window.contentView; CustomView *view = [[CustomView alloc] init]; [self.view addSubview:view]; view.translatesAutoresizingMaskIntoConstraints = NO; // Set vertical and horizontal alignments and view size // A little trick from here https://github.com/evgenyneu/center-vfl NSDictionary *views = NSDictionaryOfVariableBindings(_view, view); [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_view]-(<=1)-[view(200)]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_view]-(<=1)-[view(200)]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]]; } @end 

versión de iOS

 @implementation MyView #define SHADOW_SIZE 10 - (void)drawRect:(CGRect)dirtyRect { UIBezierPath *path; [[UIColor networkingColor] set]; path = [UIBezierPath bezierPathWithRect:dirtyRect]; [path fill]; // Draw a shadow CGRect rect = CGRectMake(SHADOW_SIZE, SHADOW_SIZE, dirtyRect.size.width - SHADOW_SIZE, dirtyRect.size.height - SHADOW_SIZE); path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:32]; [[[UIColor blackColor] colorWithAlphaComponent:0.3f] set]; [path fill]; // Draw shape rect.origin = CGPointMake(0, 0); path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:32]; [[UIColor orangeColor] set]; [path fill]; } #define USE_ALIGNMENT_RECTS 1 #define USE_ALIGNMENT_INSETS 1 #if USE_ALIGNMENT_RECTS - (CGRect)frameForAlignmentRect:(CGRect)alignmentRect { CGRect rect = alignmentRect; rect.size.width += SHADOW_SIZE; rect.size.height += SHADOW_SIZE; return rect; } - (CGRect)alignmentRectForFrame:(CGRect)frame { CGRect rect = frame; rect.size.width = frame.size.width - SHADOW_SIZE; rect.size.height = frame.size.height - SHADOW_SIZE; return rect; } #endif #if USE_ALIGNMENT_INSETS - (UIEdgeInsets)alignmentRectInsets { return UIEdgeInsetsMake(0, 0, SHADOW_SIZE, SHADOW_SIZE); } #endif @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; MyView *view = [[MyView alloc] init]; [self.view addSubview:view]; view.translatesAutoresizingMaskIntoConstraints = NO; UIView *superview = self.view; NSDictionary *views = NSDictionaryOfVariableBindings(superview, view); [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[superview]-(<=1)-[view(200)]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[superview]-(<=1)-[view(200)]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]]; } @end 

Como puedes ver, la versión de iOS también tiene -alignmentRectInsets: que funciona bien, pero quiero averiguar qué -frameForAlignmentRect: con los -frameForAlignmentRect: y -alignmentRectForFrame:

necesitas cambiar tu código de:

 view.translatesAutoresizingMaskIntoConstraints = NO; 

A:

 self.view.translatesAutoresizingMaskIntoConstraints = NO; 

Esto establece si la máscara de autorrealización de la vista debe traducirse en restricciones para el sistema de layout basado en restricciones.

Creo que en tu ejemplo tienes que trabajar con USE_ALIGNMENT_INSETS, es más fácil trabajar con

 - (UIEdgeInsets)alignmentRectInsets 

De todos modos, si tiene intención de usar USE_ALIGNMENT_RECTS, si depura después de mi cambio propuesto, verá que el tamaño de la subvista es 0 en

 - (CGRect)alignmentRectForFrame:(CGRect)frame 

Entonces, me gustaría recomendarte que establezcas un marco inicial

 view.frame = CGRectMake(50,50,200,200); 

Pero no es fácil, necesita calcular muy bien la position o usar más restricciones para permitir la vista en el centro de la supervisión.

Otra cosa son las restricciones, aquí está utilizando el código que estará en desuso:

 - (void)addConstraints:(NSArray *)constraints 

En cambio, necesitas trabajar con

 NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[superview]-(<=1)-[view(200)]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]; NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[superview]-(<=1)-[view(200)]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]; NSArray *constraints = [[NSArray arrayWithArray:horizontalConstraints] arrayByAddingObjectsFromArray:verticalConstraints]; [NSLayoutConstraint activateConstraints:constraints]; 

Gracias.