iOS 10-11上的NSLayoutConstraint动画问题 [英] NSLayoutConstraint animation issue on iOS 10-11

查看:195
本文介绍了iOS 10-11上的NSLayoutConstraint动画问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为视图实现显示/隐藏动画。
的想法是调整视图的高度限制大小,并让其超级视图随之调整大小。为此,我添加了视图的高度约束,并将其底部约束固定到超级视图的底部(因此,我不需要指定超级视图的高度约束)

I'm trying to implement show/hide animation for a view. The idea is to resize view's height constraint and to let its superview to resize with it. To achieve this I add view's height constraint and also pin its bottom constraint to superview's bottom (so I do not need specifying superview's height constraint)

在iOS 9上,它按预期工作:

On iOS 9 it works as expected:

这会在iOS 10-11上发生:

And this happens on iOS 10-11:

动画代码:

#import "ViewController.h"

@interface ViewController ()
{
    BOOL _hideFlag;
    CGFloat _redViewHeight;
}

@property (strong, nonatomic) IBOutlet UIView *containerView;
@property (strong, nonatomic) IBOutlet UIButton *toggleButton;
@property (strong, nonatomic) IBOutlet UIView *redView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *redViewHeightConstraint;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [_toggleButton addTarget:self action:@selector(toggle:) forControlEvents:UIControlEventTouchUpInside];
    _redViewHeight = _redViewHeightConstraint.constant;
}

- (void)toggle:(UIButton *)sender
{
    _hideFlag = !_hideFlag;
    [_containerView layoutIfNeeded];

    [UIView animateWithDuration:0.2 animations:^{
        _redViewHeightConstraint.constant = _hideFlag ? 0 : _redViewHeight;
        [_containerView layoutIfNeeded];
    }];
}

@end

编辑

感谢@Kuldeep。只是强调一下:关键是至少在层次结构中受影响较大的视图的超级视图上调用 layoutIfNeeded 。因此,在我的情况下,由于 containerView 的高度也在变化,我不得不在 layoutIfNeeded > containerView 的超级视图。

Solved thanks to @Kuldeep. Just to emphasize: the point is to call layoutIfNeeded at least on the upper affected view's superview in the hierarchy. So in my case since the height of the containerView is also changing I had to call layoutIfNeeded on containerView 's superview.

推荐答案

尝试在iOS 9,10,11中使用它

Try this it works in iOS 9,10,11

目标C

- (IBAction)btnChangeTapped:(UIButton *)sender {
    sender.selected =! sender.selected;

    if (sender.selected) {
        [self.view layoutIfNeeded];
        [UIView animateWithDuration:1.0 animations:^{
            self.constraintHeightOfView.constant = 100.0; // as per your require
            [self.view layoutIfNeeded];
        }];
    }
    else {
        [self.view layoutIfNeeded];
        [UIView animateWithDuration:1.0 animations:^{
            self.constraintHeightOfView.constant = 350.0; // Back to Normal
            [self.view layoutIfNeeded];
        }];
    }
}

Swift 5.0

@IBAction func btnChangeTapped(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected

    if sender.isSelected {
        self.view.layoutIfNeeded()
        UIView.animate(withDuration: 1.0, animations: {
            self.constraintHeightOfView.constant = 100.0 // as per your require
            self.view.layoutIfNeeded()
        })
    } else {
        self.view.layoutIfNeeded()
        UIView.animate(withDuration: 1.0, animations: {
            self.constraintHeightOfView.constant = 350.0 // Back to Normal
            self.view.layoutIfNeeded()
        })
    }
}

这篇关于iOS 10-11上的NSLayoutConstraint动画问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆