【问题标题】:How do I set the first UIViewController's view to autoresize?如何将第一个 UIViewController 的视图设置为自动调整大小?
【发布时间】:2011-10-18 12:26:37
【问题描述】:

我有一个UIViewController 子类来处理我对仅横向应用程序的视图。我希望它的视图可以调整大小以自动反映景观尺寸,但似乎没有。但是,子视图可以。

这是复制代码。

@interface MyViewController : UIViewController {
  UIView *subview;
}

@implementation MyViewController

- (id)init
{
  self = [super init];
  if (self) {
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    subview = [[UIView alloc] initWithFrame:self.view.frame];
    subview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:subview];
  }

  return self;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
  return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
  NSLog(@"My view's frame (%f,%f)", self.view.frame.size.width, self.view.frame.size.height);
  NSLog(@"Subview's frame (%f,%f)", subview.frame.size.width, subview.frame.size.height);
}

@end

然后……

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
  self.window.rootViewController = [[MyViewController alloc] init];
  [self.window makeKeyAndVisible];
  return YES;
}

超级简单。然而,在启动时,它会记录:

My view's frame (320.000000,480.000000)
Subview's frame (480.000000,320.000000)

子视图已正确调整大小,但父视图仍有纵向框架。

如何调整顶层视图的大小以适应横向模式?

【问题讨论】:

    标签: ios uiview uiviewcontroller


    【解决方案1】:

    您必须允许窗口自动调整子视图的大小:

    window.autoresizesSubviews=YES;
    

    【讨论】:

    • 谢谢。不使用自动布局时,在属性检查器下的第一个视图上选择自动调整子视图。
    【解决方案2】:

    在你的 viewController 中使用它

    self.view.frame = [[UIScreen mainScreen] bounds];
    

    【讨论】:

      【解决方案3】:
      self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
      

      将此添加到您的 viewdidload 中。确保在 storyboard 中, Viewcontrollers view 属性被分配给 view

      【讨论】:

        【解决方案4】:
        -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        
            if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        
                //is portrait
            
                view.frame = CGRectMake(0,0,320,480)
        
            
            } else {
        
               //is landscape
        
               view.frame = CGRectMake(0,0,480,320);
              
            }
        }
        

        【讨论】:

        • 不幸的是,这只是硬编码,这是微不足道的,但我想避免。即使在当前的设备生态系统中,该代码也需要另一个分支来检查设备惯用语、电话或平板电脑。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多