如何在没有IBOutlet的情况下为NSLayoutConstraint设置动画

时间:2014-05-31 10:51:35

标签: ios objective-c autolayout iad

我有一个包含视图(另一个控制器)的视图控制器和底部的标签栏。我想添加一个iAD横幅,以便在加载和卸载广告时,相应地调整包含的视图的大小。

/* AutoLayout */
/* pin Left of child to left of parent */
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:child.view
                                                      attribute:NSLayoutAttributeLeft
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeLeft
                                                     multiplier:1.0
                                                       constant:0]];

/* pin Right of child to right of parent */
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:child.view
                                                      attribute:NSLayoutAttributeRight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeRight
                                                     multiplier:1.0
                                                       constant:0]];

/* pin top of child to bottom of nav bar(or status bar if no nav bar) */
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:child.view
                                                      attribute:NSLayoutAttributeTop
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.topLayoutGuide
                                                      attribute:NSLayoutAttributeBottom
                                                     multiplier:1.0
                                                       constant:0]];


/* pin Top of tab bar to bottom of child view */
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.bottomLayoutGuide
                                                      attribute:NSLayoutAttributeTop
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:child.view
                                                      attribute:NSLayoutAttributeBottom
                                                     multiplier:1.0
                                                       constant:0]];


/* 
 * adConstraint is an instance variable to hold the constraint I want to animate
 * Note that it is not an IBOutlet
 */
adConstraint = [NSLayoutConstraint constraintWithItem:child.view
                                            attribute:NSLayoutAttributeBottom
                                            relatedBy:NSLayoutRelationEqual
                                               toItem:self.view
                                            attribute:NSLayoutAttributeBottom
                                           multiplier:1.0
                                             constant:0];
[self.view addConstraint:adConstraint];

在我的委托方法中,我有以下

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    UIView animateWithDuration:1 animations:^{
        adConstraint.constant = -banner.frame.size.height;
        [self.view layoutIfNeeded];
    }];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    UIView animateWithDuration:1 animations:^{
        adConstraint.constant = 0;
        [self.view layoutIfNeeded];
    }];
}

我真的需要帮助!如果没有故事板和IBOutlet,或者我做某事(非常)错了,是否有可能实现这一目标?

请帮助?!

*********************** 编辑: ************ ************

我注意到了一个问题;广告显示但未调用bannerDidLoadAd。我知道这是因为我在其中添加了日志语句。有谁知道为什么会发生这种情况????

1 个答案:

答案 0 :(得分:0)

尝试拨打

[self.view setNeedsUpdateConstraints];

[self.view layoutIfNeeded];

它应该有用!

[UIView animateWithDuration:1 animations:^{
        adConstraint.constant = 0;
        [self.view setNeedsUpdateConstraints];
        [self.view layoutIfNeeded];
}];
相关问题