使用Autolayout强制UIView在导航栏下方

时间:2014-06-19 17:15:46

标签: ios7 uiview uiscrollview uinavigationbar autolayout

在viewDidLoad中,我以编程方式创建具有水平滚动的UIScrollView,将其添加到控制器的视图中,然后设置约束以将其推送到导航栏下方。但它不起作用,我缺少什么(我的自动布局知识很弱,所以我可能会遗漏多件事)。以下是代码:

    UIScrollView *scrollingView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 40)];
    self.scrollingView = scrollingThumbsScrubber;
    self.scrollingView.backgroundColor = [UIColor greenColor];

    [self.view addSubview:self.scrollingView];

    if ([self respondsToSelector:@selector(topLayoutGuide)]) {

        [self.scrollingView setTranslatesAutoresizingMaskIntoConstraints: NO];
        id topGuide = self.topLayoutGuide;
        NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (scrollingView, topGuide);


        [self.view addConstraints:
         [NSLayoutConstraint constraintsWithVisualFormat: @"V:[topGuide]-20-[scrollingView]"
                                                 options: 0
                                                 metrics: nil
                                                   views: viewsDictionary]
         ];
    }

1 个答案:

答案 0 :(得分:0)

实际发生了什么错误?

只需致电

[self.view addSubview:self.scrollingView];

并删除之后的所有代码应自动将滚动视图放在导航栏下面