objective-c - UIViewAutoresizingNone : Resize after rotation

标签 objective-c ios ipad autoresizingmask

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

- (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 定义并在 application: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 View 就位。 当我旋转 clockVC View (其 autoResizeMask 设置为 UIViewAutoresizingNon)时调整大小以占用整个屏幕。

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

这里是我的问题的截图。 旋转前: enter image description here

旋转后: enter image description here

最佳答案

不要为此使用 Root View ,而是将 subview 添加到 View Controller 的 View 中。 UIViewControllers 通常设计为填充其容器的整个空间(在本例中为窗口)。

例如,如果您要将 UIViewController 添加到 UINavigationController,导航 Controller 将接管根据导航 Controller 的布局调整 View Controller View 大小的责任(导航栏是否可见等)。我不确定 UIWindow 的行为究竟如何(我认为这不是很清楚地记录),但在 SDK 的很多地方, View Controller 的父级负责其子级的定位和大小(UIPopoverController, UITabBarController 等)。

关于objective-c - UIViewAutoresizingNone : Resize after rotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5804431/

相关文章:

iphone - iOS静态框架和静态库

ios - 删除 #example iOS 之后的文本

iphone - UIView 动画不适用于开始在 iPhone 应用程序中编辑文本

iPhone 应用程序与 iPad 的兼容性 - 我必须支持 x2 模式吗?

objective-c - 带有模态转场的 WKInterfaceController

ios - 为什么 GCD 中的组没有 dispatch_group_sync 函数?

ios - 由于未实现的 init(frame :)),用 Swift 编写的 UIStackView 子类可能会崩溃

ios - iOS Objective-c insertObject:对象atIndex:index

iphone - UITabBarController 自定义

ios - 我怎样才能普遍禁用横向?