【问题标题】:UIViewAutoresizingNone: Resize after rotationUIViewAutoresizingNone:旋转后调整大小
【发布时间】:2011-04-27 21:53:32
【问题描述】:

我有一个简单的 IPAD 结构,其中包含来自 viewController 的视图:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    ClockVC *clockVC = [[ClockVC alloc]init];
    clockVC.view.frame = CGRectMake(100, 100, clockVC.view.bounds.size.width, clockVC.view.bounds.size.height);

    [self.window addSubview:clockVC.view];
    [self.window makeKeyAndVisible];

    return YES;
}

clockVC 有一个由这段代码定义的 viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.autoresizingMask = UIViewAutoresizingNone;
    self.view.autoresizesSubviews = UIViewAutoresizingNone;
}

clockVC 的边界由 IB 定义并在应用程序中覆盖:didFinishLaunching... 宽度和高度分别为 200 和 150。

ClockVC 实现了一步自动旋转的方法: /

/ Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{  
     [UIView animateWithDuration:0.5 
                     animations:^{self.view.alpha = 0;}
     ];
}

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ 
    [UIView animateWithDuration:1 
                     animations:^{self.view.alpha = 1;}
     ];
}

在第一次加载时,页面被正确查看,clockVC 视图就位。 当我旋转clockVC视图(其autoResizeMask设置为UIViewAutoresizingNon)调整大小以占据整个屏幕。

为什么?我想保持初始大小。

这里是我的问题的截图。 旋转前:

旋转后:

【问题讨论】:

  • 我刚刚将 clockVC.view.autoresizingMask = UIViewAutoresizingNone; 添加到应用程序:didifinishlaunching into app delegate... 但没有任何改变。

标签: objective-c ios ipad autoresizingmask


【解决方案1】:

不要为此使用根视图,而是将子视图添加到视图控制器的视图中。 UIViewControllers 通常被设计为填充其容器的整个空间(本例中为窗口)。

例如,如果您将UIViewController 添加到UINavigationController,导航控制器将负责根据导航控制器的布局(导航栏是否可见等)来调整视图控制器的视图大小。我不确定 UIWindow 的行为究竟如何(我认为这不是很清楚的记录),但在 SDK 中的很多地方,视图控制器的父级负责其子级的定位和大小(UIPopoverController,@ 987654325@ et.al.)。

【讨论】:

  • 为每个位置创建 2 个 nib 文件不是更容易吗?
  • 谢谢 omz,我在文档中找不到关于此的内容,但我只阅读了这个:In an iOS application, the window object does much of the work associated with changing the current orientation. However, it works in conjunction with the application’s view controllers to determine whether an orientation change should occur at all, and if so, what additional methods should be called to respond to the change. Specifically, it works with the view controller whose root view was most recently added to, or presented in, the window
猜你喜欢
  • 1970-01-01
  • 2019-07-11
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-05
相关资源
最近更新 更多