簡體   English   中英

當設備方向從縱向更改為橫向時,使用新的uiview填充整個橫向屏幕

[英]populate entire landscape screen with new uiview upon device orientation change from portrait to landscape

因此,我的iPhone應用程序當前具有一個tabviewcontroller,可填充整個屏幕。 該應用程序僅以縱向模式運行。 我的任務一直是檢測設備方向的變化,一旦它變為橫向,就有一個新的uiview填充整個屏幕。

我已經可以使用設備方向更改檢測了。 一旦檢測到方向更改,我就使用NSNotificationCenter成功調用了一個輔助方法deviceOrientationChanged。 如果更改為橫向模式,則運行特定的代碼塊。

在這段代碼中,我已經嘗試了各種方法,但沒有一事成功。 簡單地說self.view = newViewThing; 無法使用,因為狀態欄仍位於頂部,而選項卡仍位於底部。 我還嘗試過將此newViewThing作為子視圖添加到UIWindow。 這不起作用,因為在添加視圖時,它的方向不正確。

問題是:一旦檢測到設備方向變化,是否有辦法加載全新的uiview? 先感謝您。

是的,有一種加載新視圖的方法。 我是這樣在我的應用中制作的:

- (void)orientationChanged:(NSNotification *)notification
{
    // We must add a delay here, otherwise we'll swap in the new view
    // too quickly and we'll get an animation glitch
    [self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}

- (void)updateLandscapeView
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
    {
        [self presentModalViewController:self.landscapeView animated:YES];
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
    {
        [self dismissModalViewControllerAnimated:YES];
        isShowingLandscapeView = NO;
    }    
}

而且我還將此代碼添加到viewDidLoad

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
                                             name:UIDeviceOrientationDidChangeNotification object:nil];

和此代碼dealloc分配:

[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM