注意:这可能是添加到根视图控制器时Subview不自动调整大小的副本
我有一个iPad应用程序在其主窗口中切换不同的视图.视图切换代码如下所示:
- (void)switchToViewController:(UIViewController*)viewController { if (currentViewController != viewController) { [currentViewController.view removeFromSuperview]; currentViewController = viewController; [window addSubview:viewController.view]; } }
问题是当新视图(UISplitView)以横向方向显示时,其大小不足以填充整个窗口.右边有一个空的黑色大空间.看起来视图只有768像素宽,而不是景观窗口的1024像素宽度.
如果我将设备旋转为纵向然后返回横向,则视图会自行调整大小.
如果设备处于纵向,一切正常.如果它是我展示的第一个视图,UISplitView也会正确调整大小.如果我在横向显示另一个视图后切换到它,则只会出现此问题.
那么,是否有某种方法可以强制iPhone OS在将视图添加到窗口后调整视图大小?
我已经打过电话sizeToFit
,和setNeedsLayout
.我也尝试将视图设置为bounds
窗口bounds
,我尝试将其设置frame
为匹配上一个视图的帧.
这绝对是可能的!:-)
你可以在这里查看我的回购:https: //github.com/hfossli/AGWindowView
它将自动处理任何旋转和帧更改,因此您不必担心这一点.
如果您想担心这一点,那么您可以剪切并粘贴最重要的部分
#1将视图添加到窗口
[[UIApplication sharedApplication] keyWindow] addSubview:aView];
#2添加监听器和更新视图
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameOrOrientationChanged:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameOrOrientationChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
请记住删除通知收听
[[NSNotificationCenter defaultCenter] removeObserver:self];
#3算一算
- (void)statusBarFrameOrOrientationChanged:(NSNotification *)notification { /* This notification is most likely triggered inside an animation block, therefore no animation is needed to perform this nice transition. */ [self rotateAccordingToStatusBarOrientationAndSupportedOrientations]; } - (void)rotateAccordingToStatusBarOrientationAndSupportedOrientations { UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation; CGFloat angle = UIInterfaceOrientationAngleOfOrientation(statusBarOrientation); CGFloat statusBarHeight = [[self class] getStatusBarHeight]; CGAffineTransform transform = CGAffineTransformMakeRotation(angle); CGRect frame = [[self class] rectInWindowBounds:self.window.bounds statusBarOrientation:statusBarOrientation statusBarHeight:statusBarHeight]; [self setIfNotEqualTransform:transform frame:frame]; } - (void)setIfNotEqualTransform:(CGAffineTransform)transform frame:(CGRect)frame { if(!CGAffineTransformEqualToTransform(self.transform, transform)) { self.transform = transform; } if(!CGRectEqualToRect(self.frame, frame)) { self.frame = frame; } } + (CGFloat)getStatusBarHeight { UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if(UIInterfaceOrientationIsLandscape(orientation)) { return [UIApplication sharedApplication].statusBarFrame.size.width; } else { return [UIApplication sharedApplication].statusBarFrame.size.height; } } + (CGRect)rectInWindowBounds:(CGRect)windowBounds statusBarOrientation:(UIInterfaceOrientation)statusBarOrientation statusBarHeight:(CGFloat)statusBarHeight { CGRect frame = windowBounds; frame.origin.x += statusBarOrientation == UIInterfaceOrientationLandscapeLeft ? statusBarHeight : 0; frame.origin.y += statusBarOrientation == UIInterfaceOrientationPortrait ? statusBarHeight : 0; frame.size.width -= UIInterfaceOrientationIsLandscape(statusBarOrientation) ? statusBarHeight : 0; frame.size.height -= UIInterfaceOrientationIsPortrait(statusBarOrientation) ? statusBarHeight : 0; return frame; } CGFloat UIInterfaceOrientationAngleOfOrientation(UIInterfaceOrientation orientation) { CGFloat angle; switch (orientation) { case UIInterfaceOrientationPortraitUpsideDown: angle = M_PI; break; case UIInterfaceOrientationLandscapeLeft: angle = -M_PI_2; break; case UIInterfaceOrientationLandscapeRight: angle = M_PI_2; break; default: angle = 0.0; break; } return angle; } UIInterfaceOrientationMask UIInterfaceOrientationMaskFromOrientation(UIInterfaceOrientation orientation) { return 1 << orientation; }
祝好运!