2013-09-27 70 views
0

我有:我缺少什麼設置iOS背景?

[一系列if語句將backgroundImage設置爲[UIImageNamed @「Background-Default-Landscape」];我是正確的假設,該設備將尋找名爲[email protected]的文件,如果它是一個Retina顯示屏?]

UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage]; 
CGRect containerRect = CGRectZero; 
containerRect.size = [backgroundImage size]; 
UIView *containerView = [[UIView alloc] initWithFrame:containerRect]; 
[containerView addSubview:backgroundView]; 

現在我的應用程序啓動,一旦加載顯示一個白色屏幕,而不是指定的背景(沒有背景是純白色的)。

在我開始把東西放在的背景之前,我在缺少什麼東西來繪製背景?

感謝,

- 編輯 -

至於代碼,我有:

- (void) renderScreen 
{ 
    int height = [[UIScreen mainScreen] bounds].size.height; 
    int width = [[UIScreen mainScreen] bounds].size.width; 

    UIImage *backgroundImage = nil; 

    if (height == 2048 || height == 2008 || height == 1024 || height == 1004 || height == 984) 
    { 
     backgroundImage = [UIImage imageNamed:@"Background-Default-Portrait"]; 
    } 

    // Other such statements cut. 
    UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage]; 
    CGRect containerRect = CGRectZero; 
    containerRect.size = [backgroundImage size]; 
    UIView *containerView = [[UIView alloc] initWithFrame:containerRect]; 

    [containerView addSubview:backgroundView]; 
    [self.view addSubview:containerView]; 

然後下面是報表上繪製背景。當它旋轉

該方法被調用時的初始視圖載荷和:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self renderScreen]; 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(renderScreen) name:UIDeviceOrientationDidChangeNotification object:nil]; 
} 

的行爲是正確的初始畫面加載,然後它消失爲白色,沒有什麼有趣的似乎後,這種情況發生。

- 編輯 -

在頂部,我有:

int height = [[UIScreen mainScreen] bounds].size.height; 
int width = [[UIScreen mainScreen] bounds].size.width; 

當我的NSLog的高度和寬度,在橫向模式視網膜設備,我得到的1024的高度和寬度爲768.顯示的圖像是肖像圖像的旋轉;如果我將設備轉到側面,則圖像會整齊地填滿整個屏幕,但背景會顯示水平擠壓的圖像。

我應該做任何不同的事情來正確地獲得高度和寬度?我希望在橫向的設備的高度將是768,寬度是1024

+0

這一切似乎都是正確的。我們需要看到更多的代碼。你在哪種方法中添加視圖?並添加'containerView'到哪個視圖? –

+0

你能把更多的代碼?我建議你將containerView添加到self.view中。此外,非視網膜圖像是Default-Background-Labdscape.png(帶有.png擴展名)。 – RFG

+0

@GuyKogus,謝謝你;我在代碼中添加了更多的上下文。 – JonathanHayward

回答

1

每次renderScreen觸發將兩個子視圖添加到當前控制器的主要觀點:

[containerView addSubview:backgroundView]; 
[self.view addSubview:containerView]; 

而且每次旋轉設備時都會觸發。

至少有containerView作爲屬性並將其替換。

@property (nonatomic, strong) UIView *containerView; 

和內部- (無效)renderScreen

[self.containerView removeFromSuperview]; 
self.containerView = [[UIView alloc] initWithFrame:containerRect]; 
[self.view addSubview:self.containerView]; 

您還可以添加:

[self.view setNeedsDisplay]; 

所以該視圖是重繪。

+0

謝謝;我看到不同的行爲。它從黑色開始,然後變爲白色。 (這是真或無[self.containerView setNeedsDisplay];或[self.view setNeedsDisplay];) – JonathanHayward

+0

你在哪裏設置橫向圖像? 如果屏幕高度不是其中之一:(高度== 2048 ||高度== 2008 ||高度== 1024 ||高度== 1004 ||高度== 984)則backgroundImage仍爲零 –

+0

代碼是我碰到的一個難題。在縱向模式下,高度爲2048,寬度爲1536 ...並且在橫向模式下,高度仍然是2048,寬度仍然是1536.「if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication] .statusBarOrientation))」是一個測試,真的很高,或者身高真的很寬。 – JonathanHayward