如何设置视图的背景图像?

时间:2010-01-30 10:55:49

标签: iphone objective-c uiview uiimageview

我是Obj-C / Cocoa Touch / iPhone OS的初学者。

每次调用视图时,我希望我的应用程序有不同图像的背景。

说我有10张图片。我曾经这样用过:

//random image generation
NSString* imageName;
int aRandomNumber = arc4random() % 10;
imageName =[NSString stringWithFormat:@"g%d.jpg",aRandomNumber]; 
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:imageName]]];
NSLog(@"aRandomNumber is %d", aRandomNumber);
//random image is generated

工作正常

  • 现在,我说我的视图上有文字标签,由于图像颜色,文字显示不正确。 我怎么能让它透明一点? (我猜在Interface Builder中称为alpha。)
  • 说我的图片不是320x480。如何设置它以填充整个视图?

如何使用UIView / UIImageView进行操作?

我在文档中找到了initWithHue:saturation:brightness:alpha:,但它无效:

self.view.backgroundColor = [[UIColor alloc] initWithHue:0.0 saturation:1.0 brightness:1.0 alpha:1.0];

请帮助!


一位朋友建议........

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:imageName]]];

..........他告诉它更有效率,因为它不会将图像保存在缓存中。

7 个答案:

答案 0 :(得分:81)

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName.png"]];

more info示例项目

答案 1 :(得分:38)

除了这里的所有其他回复,我真的不认为以这种方式使用backgroundColor是正确的做事方式。就个人而言,我会创建一个UIImageView并将其插入到您的视图层次结构中。您可以将其插入顶视图并使用sendSubviewToBack将其一直推到后面:或者您可以将UIImageView作为父视图。

我不担心每个实现在这一点上的效率,因为除非你真正看到一个问题,否则它并不重要。您现在的首要任务应该是编写您可以理解并且可以轻松更改的代码。创建UIColor以用作背景图像并不是最明智的方法。

答案 2 :(得分:24)

使用此

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Default"]];

答案 3 :(得分:6)

简单的方法:

   -(void) viewDidLoad {
   self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage       imageNamed:@"background.png"]];
    [super viewDidLoad];
    }

答案 4 :(得分:2)

在不规则且不断变化的背景上直接显示任何文字是一个非常糟糕的主意。无论你做什么,有些时候文本都难以阅读。

最好的设计是将标签放在恒定的背景上,图像会在后面改变。

您可以将标签背景颜色从clear设置为白色,并将from alpha设置为50.0,您将获得漂亮的半透明效果。唯一的问题是标签的背景是一个明显的矩形。

要获得带有圆角背景的标签,您可以使用禁用了用户交互的按钮,但用户可能会将其误认为是按钮。

最好的方法是创建所需标签背景的图像,然后将其放在图像视图中,并将带有默认透明背景的标签放在其上。

普通UIViews没有图像背景。相反,您应该将UIImageView作为主视图,然后通过其图像属性旋转图像。如果将UIImageView的模式设置为“Scale to fit”,它将缩放任何图像以适合视图的边界。

答案 5 :(得分:1)

您希望主视图的背景颜色是半透明的吗?它背后没有任何东西......所以不会发生任何事情:

如果要修改任何视图的alpha,请使用alpha属性:

UIView *someView = [[UIView alloc] init];
...
someView.alpha = 0.8f; //Sets the opacity to 80%
...

视图本身具有alpha透明度,而不仅仅是UIColor。

但是因为你的问题是你无法阅读图像上的文字......或者:

  1. [DESIGN]重新考虑图像的设计/放置。它们是否需要作为背景图像?如何放置标签?
  2. [CODE]这不是最好的解决方案,但你可以做的是创建一个UIView,其框架占据整个页面并为其添加一些alpha透明度。这将创建各种各样的“叠加”。
  3. UIView *overlay = [[[UIView alloc] init] autorelease];
    overlay.frame = self.view.bounds;
    overlay.alpha = 0.2f;
    [self.view addSubview:overlay];
    ... Add the rest of the views
    

答案 6 :(得分:1)

您可以使用自定义方法在每个视图中设置多个背景图像,如下所示。

使用背景图片名称和其他颜色为每个主题制作plist

#import <Foundation/Foundation.h>
@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

可以通过

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