objective-c 如何设置视图的背景图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2167623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:52:01  来源:igfitidea点击:

How to set background image of a view?

iphoneobjective-cuiviewuiimageview

提问by Daniel

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

我是 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 images. I 've used it like this:

假设我有 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

Its working fine

它的工作正常

  • Now, say I have text labels on my view and the text isn't displaying correctly due to image colors. How can I make it a little transparent? (I guess in Interface Builder its called alpha.)
  • Say my image isn't 320x480. How do I set it to fill the entire view?
  • 现在,假设我的视图上有文本标签,但由于图像颜色,文本无法正确显示。我怎样才能让它有点透明?(我猜在 Interface Builder 中它被称为 alpha。)
  • 假设我的图像不是 320x480。如何设置它以填充整个视图?

How can I do it with UIView/UIImageView?

我如何使用 UIView/UIImageView 做到这一点?

I found initWithHue:saturation:brightness:alpha:in the documentation but it's not working:

initWithHue:saturation:brightness:alpha:在文档中找到,但它不起作用:

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

Please Help!

请帮忙!



A friend suggested........

一个朋友建议......

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

..........he told it's more efficient because it doesn't save the image in the cache.

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

回答by Gurumoorthy Arumugam

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

more infowith example project

示例项目的更多信息

回答by Dennis Munsie

Besides all of the other responses here, I really don't think that using backgroundColor in this way is the proper way to do things. Personally, I would create a UIImageView and insert it into your view hierarchy. You can either insert it into your top view and push it all the way to the back with sendSubviewToBack: or you can make the UIImageView the parent view.

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

I wouldn't worry about things like how efficient each implementation is at this point because unless you actually see an issue, it really doesn't matter. Your first priority for now should be writing code that you can understand and can easily be changed. Creating a UIColor to use as your background image isn't the clearest method of doing this.

我不会担心此时每个实现的效率如何,因为除非您确实看到了问题,否则这真的无关紧要。您现在的首要任务应该是编写您可以理解并且可以轻松更改的代码。创建一个 UIColor 作为你的背景图片并不是最清晰的方法。

回答by Saad

use this

用这个

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

回答by megha

simple way :

简单的方法:

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

回答by TechZen

It's a very bad idea to directly display any text on an irregular and ever changing background. No matter what you do, some of the time the text will be hard to read.

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

The best design would be to have the labels on a constant background with the images changing behind that.

最好的设计是将标签放在一个恒定的背景上,而后面的图像会发生变化。

You can set the labels background color from clear to white and set the from alpha to 50.0 you get a nice translucent effect. The only problem is that the label's background is a stark rectangle.

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

To get a label with a background with rounded corners you can use a button with user interaction disabled but the user might mistake that for a button.

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

The best method would be to create image of the label background you want and then put that in an imageview and put the label with the default transparent background onto of that.

最好的方法是创建你想要的标签背景的图像,然后把它放在一个图像视图中,然后把带有默认透明背景的标签放在上面。

Plain UIViews do not have an image background. Instead, you should make a UIImageView your main view and then rotate the images though its image property. If you set the UIImageView's mode to "Scale to fit" it will scale any image to fit the bounds of the view.

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

回答by sinh99

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

使用背景图像名称和其他颜色为每个主题制作 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

Can use this via

可以通过这个使用

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

回答by Malaxeur

You want the background color of your main view to be semi-transparent? There's nothing behind it... so nothing will really happen however:

您希望主视图的背景颜色是半透明的吗?它背后什么也没有……所以什么都不会发生:

If you want to modify the alpha of any view, use the alpha property:

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

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

Views themselves have the alpha transparency, not just UIColor.

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

But since your problem is that you can't read text on top of the images... either:

但是由于您的问题是您无法阅读图像顶部的文本......或者:

  1. [DESIGN] Reconsider the design/placement of the images. Are they necessary as background images? What about the placement of the labels?
  2. [CODE] It's not exactly the best solution, but what you could do is create a UIView whose frame takes up the entire page and add some alpha transparency to it. This will create an "overlay" of sorts.
  1. [设计] 重新考虑图像的设计/放置。它们是否需要作为背景图像?标签的位置如何?
  2. [代码] 这不是最好的解决方案,但您可以做的是创建一个 UIView,其框架占据整个页面并为其添加一些 alpha 透明度。这将创建各种“覆盖”。
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
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