How to set background image of a view?

后端 未结 7 1275
梦如初夏
梦如初夏 2020-12-04 16:35

I am a beginner at Obj-C/Cocoa Touch/iPhone OS.

I wish to have a background for my app with different images everytime the the view is called.

Say I have 10

7条回答
  •  时光说笑
    2020-12-04 17:23

    You can set multiple background image in every view using custom method as below.

    make plist for every theam with background image name and other color

    #import 
    @interface ThemeManager : NSObject
    @property (nonatomic,strong) NSDictionary*styles;
    + (ThemeManager *)sharedManager;
    -(void)selectTheme;
     @end
    
                 #import "ThemeManager.h"
    
                @implementation ThemeManager
                @synthesize styles;
                + (ThemeManager *)sharedManager
                {
                    static ThemeManager *sharedManager = nil;
                    if (sharedManager == nil)
                    {
                        sharedManager = [[ThemeManager alloc] init];
                    }
                    [sharedManager selectTheme];
                    return sharedManager;
                }
                - (id)init
                {
                    if ((self = [super init]))
                    {
    
                    }
                    return self;
                }
                -(void)selectTheme{
                    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                    NSString *themeName = [defaults objectForKey:@"AppTheme"] ?: @"DefaultTheam";
    
                    NSString *path = [[NSBundle mainBundle] pathForResource:themeName ofType:@"plist"];
                    self.styles = [NSDictionary dictionaryWithContentsOfFile:path];
                }
                @end
    

    Can use this via

     NSDictionary *styles = [ThemeManager sharedManager].styles;
     NSString *imageName = [styles objectForKey:@"backgroundImage"];
    [imgViewBackGround setImage:[UIImage imageNamed:imageName]];
    

提交回复
热议问题