UIScrollView不会使用AutoLayout滚动

时间:2015-02-27 04:58:20

标签: ios objective-c iphone uiscrollview autolayout

使用Autolayout时,我无法使UIScrollView垂直滚动,我尝试了很多建议。我已经阅读了Apple的technical note,但即便如此,这似乎与我的纯自动布局方法无关。

这是我的简单代码,其中我添加了两个UIView块1& 2在容器视图中。容器视图本身是UIScrollView的唯一子项,正如许多在线参考文献所建议的那样。我将第1和第2块的高度设置为每个800点,但滚动视图不会滚动。

    - (void)viewDidLoad {
        [super viewDidLoad];


        // Test UIScrollView with Autolayout, scrolling should word

        UIView *mainView = self.view;

        UIScrollView* scrollView = [UIScrollView new];
        scrollView.translatesAutoresizingMaskIntoConstraints = NO;
        scrollView.backgroundColor = [UIColor redColor];
        [self.view addSubview:scrollView];

        UIView* contentView = [UIView new];
        contentView.translatesAutoresizingMaskIntoConstraints = NO;
        contentView.backgroundColor = [UIColor greenColor];
        [scrollView addSubview:contentView];

        UIView* block1 = [[UIView alloc] init];
        block1.translatesAutoresizingMaskIntoConstraints = NO;
        [block1 setBackgroundColor:[UIColor blackColor]];
        [contentView addSubview:block1];

        UIView* block2 = [[UIView alloc] init];
        block2.translatesAutoresizingMaskIntoConstraints = NO;
        [block2 setBackgroundColor:[UIColor blackColor]];
        [contentView addSubview:block2];


        NSDictionary* viewDict = NSDictionaryOfVariableBindings(mainView,scrollView, contentView, block1, block2);

        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:0 views:viewDict]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics:0 views:viewDict]];


        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView(==mainView)]|" options:0 metrics:0 views:viewDict]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView(==mainView)]|" options:0 metrics:0 views:viewDict]];


        [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[block1(==300)]" options:0 metrics:0 views:viewDict]];
        [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[block1(==800)]" options:0 metrics:0 views:viewDict]];

        [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[block2(==300)]" options:0 metrics:0 views:viewDict]];
        [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-810-[block2(==800)]" options:0 metrics:0 views:viewDict]];

}

3 个答案:

答案 0 :(得分:0)

查看您的代码,您的内容视图的大小似乎与UIView的大小相同。

e.g。 :IF设备屏幕高度为568,那么您的内容视图大小将为568,并且您将 block2 设置为y位置= 180。

您的代码:

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView(==mainView)]|" options:0 metrics:0 views:viewDict]];

更改:

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView(2000)]|” options:0 metrics:0 views:viewDict]];

希望它能解决您的问题,如果有任何进一步的问题请告诉我。

答案 1 :(得分:0)

如果你想做的就是让滚动视图与自动布局一起使用,你可以试试这个。 http://natashatherobot.com/ios-autolayout-scrollview/

答案 2 :(得分:0)

在代码中执行以下更改

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|" options:0 metrics:0 views:viewDict]];

[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-810-[block2(==800)]|" options:0 metrics:0 views:viewDict]];

希望这有帮助