UIPresentationController preferredContentSize 更新动画

Dam*_*anD 5 animation uikit ios uipresentationcontroller

所以我有一个非常基本的 UIPresentationController,它基本上以屏幕为中心显示 contentView,它的大小由视图控制器 preferredContentSize 决定。(它非常类似于以 FormSheet 形式呈现的常规模式视图控制器)。

我想要实现的是能够动态更新此视图控制器视图的大小,只需更改它的 preferredContentSize。

当我设置 preferredContentSize 时,我的 UIPresentationController 子类在以下位置接收有关它的信息:

-(void)preferredContentSizeDidChangeForChildContentContainer:(id<UIContentContainer>)container
Run Code Online (Sandbox Code Playgroud)

但是我如何从这里用动画调整视图框架的大小?如果我只是打电话:

-(void)preferredContentSizeDidChangeForChildContentContainer:(id<UIContentContainer>)container
{
    [UIView animateWithDuration:1.0 animations:^{
        self.presentedView.frame = [self frameOfPresentedViewInContainerView];
    } completion:nil]; 
}
Run Code Online (Sandbox Code Playgroud)

立即被调用 containerViewWillLayoutSubviews 并且框架在没有动画的情况下被改变。

-(void)containerViewWillLayoutSubviews
{
    self.presentedView.frame = [self frameOfPresentedViewInContainerView];
}
Run Code Online (Sandbox Code Playgroud)

请帮我找到一种方法,用动画调整它的大小。这一定是可能的,因为它会随着动画调整大小,例如在发生旋转时。

Nic*_* S. 1

您必须使用自动布局。根据容器高度限制制作一个出口:

@property (nonatomic, strong, readwrite) IBOutlet NSLayoutConstraint *drawerViewHeightConstraint;
Run Code Online (Sandbox Code Playgroud)

然后添加以下代码:

- (void)preferredContentSizeDidChangeForChildContentContainer:(id<UIContentContainer>)container
{
    [super preferredContentSizeDidChangeForChildContentContainer:container];
    self.drawerViewHeightConstraint.constant = container.preferredContentSize.height;

    [UIView animateWithDuration:0.25
                     animations:^{
                       [self.view layoutIfNeeded];
                     }];
}
Run Code Online (Sandbox Code Playgroud)