iphone - 添加到窗口后自动调整 UIView 的大小

标签 iphone uiview ipad uiwindow

注意:这可能与Subview Doesnt AutoSize When Added to Root View Controller重复。

<小时/>

我有一个 iPad 应用程序,可以在其主窗口的不同 View 之间切换。 View 切换代码如下所示:

- (void)switchToViewController:(UIViewController*)viewController {
    if (currentViewController != viewController) {
        [currentViewController.view removeFromSuperview];
        currentViewController = viewController;
        [window addSubview:viewController.view];
    }
}

问题是,当新 View (UISplitView)以横向显示时,它的大小无法填充整个窗口。右侧有一大片空白的黑色空间。看起来 View 只有 768 像素宽,而不是横向窗口的 1024 像素宽。

如果我将设备旋转为纵向,然后返回横向, View 会自动调整大小。

如果设备处于纵向,则一切正常。如果 UISplitView 是我显示的第一个 View ,它的大小也会正确调整。仅当我在横向显示另一个 View 后切换到它时,才会出现此问题。

那么,有什么方法可以强制 iPhone 操作系统在将 View 添加到窗口后调整 View 大小吗?

我尝试调用 sizeToFitsetNeedsLayout。我还尝试将 View 的边界设置为窗口的边界,并且尝试设置框架以匹配前一个 View 的框架。

最佳答案

这绝对是可能的! :-)

您可以在这里查看我的存储库: https://github.com/hfossli/AGWindowView

它将自动处理任何旋转和帧更改,因此您不必担心。

如果您想担心这一点,那么您可以剪切并粘贴最重要的部分

# 1 将 View 添加到窗口

[[UIApplication sharedApplication] keyWindow] addSubview:aView];

#2 添加监听器并更新 View

[[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;
}

祝你好运!

关于iphone - 添加到窗口后自动调整 UIView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2659400/

相关文章:

iphone - iPhone iOS是否可以渲染弯曲的UIView?

iphone - 使用 iOS 7 在 iPad 上以 2x 模式启动应用程序

iphone - 如何在另一个 UIView Controller 中显示复杂的 UIViewController?

iphone - 增强现实 iOS : marker detection and 3D projection

ios - 在 uiviewcontroller 中添加 subview 使其正常运行

ios - 嵌套参数化闭包参数异常

iphone - 如何阻止 iOS Safari 网页在添加到主屏幕后加载缓存页面?

javascript - Jquery 移动数据-rel ="dialog"在 iphone 或 ipad 上不起作用

iphone - 我如何从 iphone 应用程序获取所有日历及其事件?

iphone - 内存管理addSubview :