2011-12-06 74 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