iOS 8 Form Sheet 模态对话框显示后调整大小

iOS 8 Form Sheet Modal Dialog Resizing After Display

我在一个应用程序上工作,它通过使用表单 sheet 模式样式执行 segue 然后显示水平翻转动画来显示模式弹出窗口。在 iOS 7 中,通过以下调用使表单占据正确的屏幕尺寸:

self.view.superview.bounds = CGRectMake(0,0,300,300);

例如,这会将模态对话框的尺寸设置为正确的大小。此模态对话框中还有一个按钮,允许用户查看 'more detail',为此视图会增长以显示一些附加内容。我们通过动画视图再次做到了这一点,并调用将边界设置为在超级视图上稍微大一点的东西。

例如:

self.view.superview.bounds = CGRectMake(0,0,600,600);

在 iOS 7 中,这非常有效。在 iOS 8 中,它已停止工作。我们现在必须调用:

而不是修改超级视图的边界
self.preferredContentSize = CGSizeMake(300, 300);

这解决了表单打开后的外观问题,但之后我找不到一种方法来操纵视图的大小。设置超级视图边界无效。

我可以在 iOS8 中看到 Apple 已对表单 sheets 进行了更改以具有 3 视图层次结构(您的视图 -> 阴影视图 -> 过渡视图 -> window) 在 iOS 7 中,它只是一个 2 视图层次结构(您的视图 -> 影子视图 -> window)。

我试过操纵超级视图的超级视图,但没有达到预期的效果。

还有其他人看到这个/找到解决方法吗?这是一个示例项目:

https://github.com/ingybing/FormSheet

运行 它在 iOS 7 模拟器中,您将看到所需的行为。这就是我在 iOS 8

中努力工作的原因

视图控制器示例:

#import "ModalWindowViewController.h"

@interface ModalWindowViewController ()
@property bool minimised;
@property bool isInitialViewLoadLayout;
@end

#define is_iOS8 ([[[UIDevice currentDevice] systemVersion]floatValue] >= 8)

@implementation ModalWindowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.minimised = YES;
    self.isInitialViewLoadLayout = YES;

    if(is_iOS8)
    {
        // Set initial size in iOS 8 etc.
        self.preferredContentSize = CGSizeMake(300, 300);
    }
}

- (void) viewWillLayoutSubviews
{
    if (self.isInitialViewLoadLayout)
    {
        self.isInitialViewLoadLayout = NO;

        if(!is_iOS8)
        {
            // Set initial size in iOS 6,7 etc.
            self.view.superview.bounds = CGRectMake(0, 0, 300, 300);
        }
    }
}

- (IBAction)minMaxTouched:(id)sender
{
    CGRect newSize = CGRectMake(0, 0, 300, 300);

    if (self.minimised == YES)
    {
        newSize = CGRectMake(0, 0, 600, 600);
    }

    if(is_iOS8)
    {
        // Doesn't Work
        // self.view.superview.bounds = newSize;

        // Doesn't seem to do anything either.
        self.preferredContentSize = newSize.size;
    }
    else
    {
        // Works a treat on ios7
        self.view.superview.bounds = newSize;
    }

    self.minimised = !self.minimised;
}

- (IBAction)closeTouched:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

经过一番折腾后.. 如果您将 view.superview 的边界更新为某些内容,然后在超级视图上调用 layoutIfNeeded。这将导致调用 willLayoutSubviews 方法。如果你在那里设置超级视图边界,它会更新模态对话框的大小,但如果你在其他地方这样做则不会。

示例视图控制器解决方案:

#import "ModalWindowViewController.h"

@interface ModalWindowViewController ()
@property bool minimised;
@property CGRect windowBounds;
@end

#define is_iOS8 ([[[UIDevice currentDevice] systemVersion]floatValue] >= 8)

@implementation ModalWindowViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Start with a minimised window size.
    self.minimised = YES;
    self.windowBounds = CGRectMake(0, 0, 300, 300);

    if(is_iOS8)
    {
        // Set initial size in iOS 8 etc.
        self.preferredContentSize = self.windowBounds.size;
    }
}

- (void) viewWillLayoutSubviews
{
    // Resize the window based on it's current required size.
    // The supeview bounds only seems to actually changed on screen
    // if changed inside this method.
    self.view.superview.bounds = self.windowBounds;
}

- (IBAction)minMaxTouched:(id)sender
{
    // Toggle the dimensions of the window bases on minimised / maximised state.
    if (self.minimised == YES)
    {
        self.windowBounds = CGRectMake(0, 0, 600, 600);
    }
    else
    {
        self.windowBounds = CGRectMake(0, 0, 300, 300);
    }

    // Set some value that will get overridden in viewWillLayoutSubviews
    // You need to change the view bounds or it won't actually invoke a layout.
    self.view.superview.bounds = CGRectMake(0, 0, 100, 100);

    // Toggle minimised state before we layout.
    self.minimised = !self.minimised;

    // Manually request a layout. Since the superview bounds have been changed
    // to a temporary value it should cause a layout where we set the real
    // desired size.
    [self.view.superview layoutIfNeeded];
}

- (IBAction)closeTouched:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end