如何以编程方式将垂直约束绑定到视图?

时间:2014-01-25 14:57:32

标签: ios ios7 nslayoutconstraint

我正在阅读将iAd应用到我的应用程序中。 Apple提供了一个基本横幅sample code

我将解决方案移植到我的UITableViewController时遇到问题。因为我不使用任何xib文件来将引入的NSLayoutConstraint绑定到我的tableview:

@property (nonatomic, strong) IBOutlet NSLayoutConstraint *bottomConstraint;

如果没有正确绑定,我将无法根据广告尺寸缩小视图:

- (void)layoutAnimated:(BOOL)animated
{
    CGRect contentFrame = self.view.bounds;

    // all we need to do is ask the banner for a size that fits into the layout area we are using
    CGSize sizeForBanner = [_bannerView sizeThatFits:contentFrame.size];

    // compute the ad banner frame
    CGRect bannerFrame = _bannerView.frame;
    if (_bannerView.bannerLoaded) {

        // bring the ad into view
        contentFrame.size.height -= sizeForBanner.height;   // shrink down content frame to fit the banner below it
        bannerFrame.origin.y = contentFrame.size.height;
        bannerFrame.size.height = sizeForBanner.height;
        bannerFrame.size.width = sizeForBanner.width;

        // if the ad is available and loaded, shrink down the content frame to fit the banner below it,
        // we do this by modifying the vertical bottom constraint constant to equal the banner's height
        //
        NSLayoutConstraint *verticalBottomConstraint = self.bottomConstraint;
        verticalBottomConstraint.constant = sizeForBanner.height;
        [self.view layoutSubviews];

    } else {
        // hide the banner off screen further off the bottom
        bannerFrame.origin.y = contentFrame.size.height;
    }

    [UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
        _contentView.frame = contentFrame;
        [_contentView layoutIfNeeded];
        _bannerView.frame = bannerFrame;
    }];
}

问题是我如何以编程方式将bottomConstraint绑定到视图的垂直底部以便之后缩小它?

0 个答案:

没有答案