Come si imposta stretchableImageWithLeftCapWidth in Interface builder?Come si imposta stretchableImageWithLeftCapWidth in Interface builder?
risposta
Il builder di interfaccia non supporta immagini estensibili.
-(void) viewDidLoad {
[super viewDidLoad];
[myImageView setImage:[[myImageView image] stretchableImageWithLeftCapWidth:5 topCapHeight:5 ]];
}
Attualmente è (a partire dalla versione 4.5) una limitazione di InterfaceBuilder. Tuttavia, la necessità di creare un IBOutlet
per ciascun pulsante e specificare manualmente che l'immagine è estensibile in viewDidLoad
non è una soluzione eccezionale, se non per altri motivi che rende la creazione dell'interfaccia utente più ingombrante e anche più fragile.
Invece di questo, creiamo una sottoclasse UIButton
che sarà sempre fare tutta l'immagine di sfondo estensibile per impostazione predefinita. In questo esempio allungo solo l'immagine di sfondo normale ed evidenziata. Per una lezione pronta per la produzione, probabilmente vorresti controllare lo tutti gli stati di entrambe le immagini di sfondo e in primo piano e allungarle.
@implementation NTSTButton
- (id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
/* There is no support in IB for stretchable images. So, let's
make sure _any_ background image assigned to this button
stretches correctly.
Note: Setting the stretch cap to 10px, which should work with
almost all button background images.
*/
UIImage *normalImage =
[[self backgroundImageForState:UIControlStateNormal]
stretchableImageWithLeftCapWidth:10 topCapHeight:10] ;
UIImage *highlightedImage =
[[self backgroundImageForState:UIControlStateHighlighted]
stretchableImageWithLeftCapWidth:10 topCapHeight:10] ;
[self setBackgroundImage:normalImage
forState:UIControlStateNormal];
[self setBackgroundImage:highlightedImage
forState:UIControlStateHighlighted];
}
return self;
}
@end
+1 grazie per la direzione – Brenden
Simile a @answerbot, ma aggiornato per iOS 5.0+ come stretchableImageWithLeftCapWidth:topCapHeight:
è ora deprecato.
@implementation UIButtonStretchable
- (id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setBackgroundImage: [[self backgroundImageForState:UIControlStateNormal] resizableImageWithCapInsets:UIEdgeInsetsMake(22,6,22,6)]forState:UIControlStateNormal];
[self setBackgroundImage: [[self backgroundImageForState:backgroundImageForState:UIControlStateHighlighted] resizableImageWithCapInsets:UIEdgeInsetsMake(22,6,22,6)]forState:backgroundImageForState:UIControlStateHighlighted];
}
return self;
}
@end
iOS7 cataloghi Asset anche introdotto in cui è possibile specificare l'area estensibile delle immagini, ma che funziona solo se solo sostenete iOS7 (vale a dire, non si preoccupano gli utenti del iPad originale, iPod touch, o 3GS; sì, non c'è frammentazione in iOS).
Usare Stretching
configurazioni in View
sezione.
Prima di tutto, modificare la modalità di visualizzazione su
Aspect fill
.Quindi modificare il campo X, Y, Larghezza, Altezza nella parte
Stretching
. Per esempio: Diciamo che avete una dimensione 10x20 dell'immagine, e vuole adattare in una 5x10 vista dimensione ==> Set X = 5/10 = 0.5, Y = 10/20 = 0,5
NOTA: IB non consente di utilizzare immagini più piccole per "allungare" in una cornice più grande, quindi è necessario progettare l'immagine più grande possibile
'Il generatore di interfaccia non supporta immagini estensibili. ==> Spiacente, ma lo fa. La parte 'Stretching' prende questo ruolo, citato nella mia risposta sotto – samthui7