TL; DR
Utilizzare la funzione UIEdgeInsetsInsetRect(theRect, theInsets)
.
Esempio
// CGRectMake takes: left, bottom, width, height.
const CGRect originalRect = CGRectMake(0, 0, 100, 50);
// UIEdgeInsetsMake takes: top, left, bottom, right.
const UIEdgeInsets insets = UIEdgeInsetsMake(10, 10, -20, -20);
// Apply the insets…
const CGRect adjustedRect = UIEdgeInsetsInsetRect(originalRect, insets);
// What's the result?
NSLog(@"%@ inset by %@ is %@",
NSStringFromCGRect(originalRect),
NSStringFromUIEdgeInsets(insets),
NSStringFromCGRect(adjustedRect));
// Logs out…
// {{0, 0}, {100, 50}} inset by {10, 10, -20, -20} is {{10, 10}, {110, 60}}
Spiegazione
- Un inserto positivo sposta il bordo del rettangolo verso l'interno (verso la metà rettangolo).
- Un riquadro negativo sposta il bordo verso l'esterno (lontano dal centro del rettangolo).
- Un inserto zero lascerà il bordo da solo.
Tell Me More
Altre funzioni utili per operare su CGRect
s sono coperti da this note.
Fantastico! Avrei dovuto aspettarmi che questo metodo esistesse, ma sono grato che non ho dovuto cercarlo a fondo! ;-) – mbm29414