【问题标题】:Autoresizing views on iPhone在 iPhone 上自动调整视图大小
【发布时间】:2026-02-10 08:40:01
【问题描述】:

我目前正在做一个项目,我需要同时支持纵向和横向。我读过几本书,它们都告诉我要做不同的事情。我想支持横向的视图很简单。它是一个 UINavigationBar 和一个附加到 viewcontrollers 视图的 UIWebview。我以编程方式制作了视图。

我用这个方法对吗:
- (void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation 持续时间:(NSTimeInterval)duration;

我已经看到,在 iOS 3 之前,还调用了另外两种方法。但是,我的应用不针对 iOS 3 以下的应用。

此代码正在运行,但我的问题是它是否是以编程方式执行此操作的正确方法,因为我不想在此视图上使用 InterfaceBuilder。

Codesn-p(工作)

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)x duration:(NSTimeInterval)duration {
   if (UIInterfaceOrientationIsLandscape(x)) {
      topbar.frame = CGRectMake(0.0f, 0.0f, 480.0f, 44.0f);
   }
}

我不需要应用任何动画是否正确,iOS 会处理这个正确的?

【问题讨论】:

    标签: iphone


    【解决方案1】:

    您可以使用以下两种方法来支持界面方向

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    
     //Set the boolean variable for interface orientation condition. 
    
    }
    
    
    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
    //Set the coordinates of the Views you are using.
    
    }
    

    是的,您不必担心动画部分,操作系统会处理它。

    【讨论】:

      【解决方案2】:

      您使用的方法是对用户界面进行自定义更改。你必须自己在那里做动画。 (这就是为什么有一个 Duration 参数)如果你只有 2 个 Views 需要调整大小,你可以设置 Autoresizing 掩码。

      即:

      webView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

      【讨论】:

      • 谢谢,但是如何使用 autoresizingMask?我试着按照你上面写的那样做,但是没有用。我在 CGRectMake 结构中设置了固定值。我应该在那里写点别的吗?
      • 如果设置了自动调整掩码,iOS 将覆盖这些。对于 WebView 认为你需要 Webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
      最近更新 更多