【问题标题】:Ensure a view remains full screen upon rotate确保视图在旋转时保持全屏
【发布时间】:2011-12-06 14:04:27
【问题描述】:

我有一个简单的UIWebView,已在viewDidLoad 方法中添加到我的UIViewController

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)

【问题讨论】:

  • 这是 iphone 还是 ipad 应用程序?您应该知道尺寸,因此您只需调整视图的大小以在旋转时填满整个屏幕。
  • 可以做到这一点,但我的印象是我可以将视图“锚定”或“停靠”到角落,这样它就会拉伸以填充为底层视图大小变化。我来自 WebForms 背景,所以我可能弄错了。

标签: iphone objective-c ios ipad orientation


【解决方案1】:

您所做的是正确的,并且在大多数情况下都应该有效。但是因为我不知道你的视图堆栈。我会建议一个确定的拍摄方式 -

- (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];
}

【讨论】:

    【解决方案2】:

    因为 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{
    }
    

    【讨论】:

    • 为什么要为这种微不足道的一对一关系使用通知?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多