2010-01-30 80 views
24

我是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]]]; 

..........他告訴它的效率更高,因爲它不保存在圖像緩存。

+0

一個朋友建議使用此..... ... self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle main Bundle] resourcePath] stringByAppendingPathComponent:imageName]]]; ..........他告訴它更高效率,它不會將圖像保存在緩存中。 – Daniel 2010-01-30 11:02:33

回答

1

你希望主視圖的背景顏色是半透明的嗎?沒有什麼背後...所以什麼都不會真正發生。然而如果你想修改任何視圖的Alpha

,使用alpha屬性:

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

意見本身有alpha透明度,而不僅僅是的UIColor。

但你的問題是,你不能閱讀圖像的頂部的文本......或者:

  1. [設計]重新考慮圖像的設計/佈局。它們是否必要作爲背景圖像?標籤的位置怎麼樣?
  2. [CODE]這不是最好的解決方案,但你可以做的是創建一個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 
2

這是一個非常糟糕的主意,直接顯示在一個不規則的和不斷變化的背景的文本。無論你做什麼,有些時候文本很難閱讀。

最好的設計是將標籤放在背景不變的背景上,使圖像背後變化。

您可以將標籤背景顏色從透明設置爲白色,並將透明度設置爲從50.0到半透明效果。唯一的問題是標籤的背景是一個嚴峻的矩形。

要獲得具有圓角背景的標籤,您可以使用禁用了用戶交互的按鈕,但用戶可能會錯誤地將其用作按鈕。

最好的方法是創建你想要的標籤背景的圖像,然後把它放在一個圖像視圖中,並將默認透明背景的標籤放在上面。

普通UIViews沒有圖像背景。相反,你應該使UIImageView成爲你的主視圖,然後通過圖像屬性旋轉圖像。如果您將UIImageView的模式設置爲「適合縮放」,它將縮放任何圖像以適合視圖的邊界。

37

除了這裏的所有其他答案,我真的不認爲以這種方式使用backgroundColor是做事的正確方法。就我個人而言,我會創建一個UIImageView並將其插入到您的視圖層次結構中。您可以將它插入到頂視圖中,並使用sendSubviewToBack將它一直推到後面:或者可以使UIImageView成爲父視圖。

我不會擔心每個實現在這個點上的效率如何,因爲除非你真的看到問題,否則它並不重要。你現在的首要任務應該是編寫你可以理解並且可以很容易改變的代碼。創建一個用作背景圖片的UIColor並不是做這件事最清晰的方法。

6

簡單的方法:

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

more info與示例項目

+0

如果我使用此代碼我得到內存泄漏如何修復 – codercat 2014-02-05 13:07:58

+0

Up投給好回答兄弟 – user3182143 2017-08-21 12:21:03

1

可以使用如下自定義的方法在每個視圖設置多個背景圖像。

與背景圖像的名稱和其他顏色的每theam化妝的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]]; 
21

使用本

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

救了我,工作很好 – 2017-03-24 13:24:40