In questo articolo vedremo come impostare un’immagine come sfondo in un’applicazione iPhone.
Setup base
Nel View Controller:
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
backgroundView.frame = self.view.frame;
[self.view addSubview:backgroundView];
[self.view sendSubviewToBack:backgroundView];
Dimensioni immagine
Risoluzioni consigliate:
- iPhone 4/4s: 960x640px
- iPhone 5/5s: 1136x640px
- iPhone 6: 1334x750px
- iPhone 6+: 1920x1080px
- iPad: 2048x1536px
Gestione memoria
Best practices:
- Usa @2x e @3x
- Ottimizza PNG
- Cache immagini
- Rilascia memoria
- Gestisci rotazione
Auto Layout
Configurazione constraints:
backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
[backgroundView.topAnchor constraintEqualToAnchor:self.view.topAnchor].active = YES;
[backgroundView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
[backgroundView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
[backgroundView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES;
Content Mode
Opzioni disponibili:
- ScaleToFill
- AspectFit
- AspectFill
- Center
- Top/Bottom/Left/Right
Performance
Ottimizzazioni:
- Compressione PNG
- Dimensioni appropriate
- Caching system
- Hardware acceleration
- Background thread
Alternative
Altri approcci:
- CALayer background
- View backgroundColor
- Pattern images
- Gradient layer
- Blur effects
Testing
Verificare su:
- Diversi dispositivi
- Orientamenti
- Condizioni low memory
- Background/Foreground
- State preservation
Best Practices
Consigli utili:
- Usa asset catalog
- Ottimizza immagini
- Gestisci memoria
- Supporta tutti i device
- Testa performance
Conclusioni
Punti chiave:
- Dimensioni corrette
- Ottimizzazione
- Auto Layout
- Memory management
- Testing accurato