UIViewAutoresizingNone: Resize after rotation

2019-07-25 06:00发布

i have a simple structure for IPAD with an AppDelegate that include a view from a 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 has a viewDidLoad defined by this code :

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

Bounds of clockVC are defined by IB and override in application:didFinishLaunching... Width and Height are respectively 200 and 150.

ClockVC implements method for one-step auto-rotation : /

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

At the first load the page is correctly viewed and clockVC view is in position. When i rotate clockVC view (whose autoResizeMask is set to UIViewAutoresizingNon)resize to take the entire screen.

Why ?? i'd like to mantain initial size.

Here screenshots of my problem. Before rotation: enter image description here

After Rotation: enter image description here

1条回答
仙女界的扛把子
2楼-- · 2019-07-25 06:36

Don't use the root view for this, add a subview to the view controller's view instead. UIViewControllers are typically designed to fill the entire space of their container (the window in this case).

For example, if you would add a UIViewController to a UINavigationController, the navigation controller takes over the responsibility to size the view controller's view according to the navigation controller's layout (whether the navigation bar is visible etc.). I'm not sure how exactly UIWindow behaves (I think this is not very clearly documented), but in a lot of places in the SDK, a view controller's parent is responsible for the positioning and size of its children (UIPopoverController, UITabBarController et.al.).

查看更多
登录 后发表回答