如何以编程方式使用自动布局在视图之间提供垂直空间?

时间:2013-04-24 21:08:28

标签: iphone autolayout nslayoutconstraint

在我的应用程序中,我必须根据每个视图垂直布局两个视图,并希望确保它们在iPhone 4和5中看起来相似。 这是代码:

nextShowView = [[WestwoodNextShowView alloc]initWithFrame:CGRectMake(-5, 322, 290, 70)];
    nextShowView.staticLabel.text = @"Next Show";
    nextShowView.date.text = @"10/25";
    nextShowView.address.text = @"Green Dot Tavern";
    nextShowView.cityState.text = @"Detroit, MI";
    [_aboutScroll addSubview:nextShowView];

    playerView = [[WestwoodMusicPlayerView alloc]initWithFrame:CGRectMake(0, 407, 320, 50)];
        [_aboutScroll addSubview:playerView];

    NSDictionary * viewsDict = NSDictionaryOfVariableBindings(nextShowView, playerView);
    NSArray * constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[nextShowView]-15-[playerView]-20-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:viewsDict];
    [_aboutScroll addConstraints:constraints];

我试图在nextShowView和playerView之间提供15个点空间,从playerView底部到视图底部边缘提供20个点空间。

请帮助!!!!

谢谢,

1 个答案:

答案 0 :(得分:0)

Autolayout仅在您提供所有每个视图需要完全指定的约束时才有效。您不仅要给出nextShowViewplayerView之间的垂直间距,不仅要考虑playerView和超视图之间的垂直间距,还要给出它们的高度,以及它们的水平位置和宽度。< / p>

如果您的观点未出现在您期望的位置,则可能没有给出足够的约束。在调试器中暂停并在控制台命令行中说po [[UIWindow keyWindow] _autolayoutTrace]以确认您是否有不明确的布局。

相关问题