2011-12-06 66 views
0

全屏我有一個簡單UIWebView,我已經加入到我的UIViewControllerviewDidLoad方法:確保視圖保持在旋轉

CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 
self.webView = [[UIWebView alloc] initWithFrame:rect]; 
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
[self.view addSubview:self.webView]; 

它看上去很不錯,但是當我旋轉手機,寬度和高度保持不變,所以現在它對於更新視圖框架來說太寬了。我也嘗試使用self.view.bounds,但它沒有任何區別。

那麼如何確保在加載時全屏視圖在旋轉時保持相同大小? (不使用IB)

+0

這是一個iphone或ipad應用程序?你應該知道尺寸,所以你可以調整視圖的大小來填充整個屏幕。 –

+0

我*可以*做到這一點,但我的印象是,我可以將視圖「錨定」或「停靠」到角落,以便隨着底層視圖大小的變化而伸展。我來自WebForms背景,所以我可能會誤解。 – powlette

回答

0

你在做什麼是正確的&應該在大多數情況下工作。但是因爲我不知道你的View Stack。我會建議一個肯定的射門方式 -

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
             duration:(NSTimeInterval)duration 
{ 
    CGRect rect; 
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft||toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 
    } 
    else 
    { 
     //some other dimensions. 
    } 
    self.webView = [[UIWebView alloc] initWithFrame:rect]; 
} 
0

由於Web視圖不僅叫一旦需要再次調用 設置新的框架

self.webView = [[UIWebView alloc] initWithFrame:rect]; 

所以你必須在viwewillappear登記通知或viewDidLoad中

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewBecamePortrait:) name:@"orientationIsPortrait" object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewBecameLandscape:) name:@"orientationIsLandscape" object:nil]; 



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ 
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) { 
     NSNotification* notification = [NSNotification notificationWithName:@"orientationIsPortrait" object:self]; 
     [[NSNotificationCenter defaultCenter] postNotification:notification]; 
    }else { 
     NSNotification* notification = [NSNotification notificationWithName:@"orientationIsLandscape" object:self]; 
     [[NSNotificationCenter defaultCenter] postNotification:notification]; 
    } 
} 

然後實現

-(void)viewBecameLandscape:(id)sender{ 
    if(webview){ 
     [webview.setframe(cgrectmake(x,y,width,height))]; 
    } 
} 
-(void)viewBecamePortrait:(id)sender{ 
} 
+0

爲什麼使用這種微不足道的1to1關係的通知? – Till