问题
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:
After Rotation:
回答1:
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.).
来源:https://stackoverflow.com/questions/5804431/uiviewautoresizingnone-resize-after-rotation