的UIScrollView和约束的问题 [英] UIScrollView and constraints issue

查看:125
本文介绍了的UIScrollView和约束的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个UIScrollView:

I create a UIScrollView:

// datasetSubView.m
UIScrollView *scrollView = [UIScrollView new];
scrollView.translatesAutoresizingMaskIntoConstraints = FALSE;
scrollView.userInteractionEnabled = TRUE;
scrollView.scrollEnabled = TRUE;
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.alpha = 0;

self.filterListView = scrollView;

然后在我的UIViewController,我添加滚动视图,并给它一个尺寸:

Then in my UIViewController, I add the scroll view and give it a size:

[self.view addSubview:self.datasetSubBar.filterListView];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[filterListView]|" options:0 metrics:nil views:@{@"filterListView": self.datasetSubBar.filterListView}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[filterListView][filtersSubBar]" options:0 metrics:nil views:@{@"filterListView": self.datasetSubBar.filterListView, @"filtersSubBar" : self.datasetSubBar.filtersSubBar}]];

[self.view layoutIfNeeded]; //gives the filterListView its frame

我然后创建一个自定义的UIView在滚动视图的地方:

I then create a custom UIView to place in the scroll view:

[self.datasetSubBar createPanels];

[self.datasetSubBar.filterListView layoutIfNeeded]; // w/out this, the panel's size is 0

//datasetSubBar.m
-(void)createPanels {
    DatasetFilterListPanelView *panel = [[DatasetFilterListPanelView alloc] init];
    panel.translatesAutoresizingMaskIntoConstraints = FALSE;
    panel.headerLabel.text = [NSString stringWithFormat:@"Panel %d", 1];
    panel.tag = 1;

    [self.filterListView addSubview:panel];

    [self.filterListView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[panel]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(panel)]];
    [self.filterListView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[panel]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(panel)]];

    self.panels = @[panel];
}

应该放在右上角子视图,但它并不:

It should place the subView in the top right corner, but it doesn't:

NSLog(@"viewDidAppear | self.datasetSubBar.filterListView: %@", self.datasetSubBar.filterListView);

NSLog(@"self.datasetSubBar.panels: %@", self.datasetSubBar.panels)

2013-02-22 16:28:22.912 [5196:14003] viewDidAppear | self.datasetSubBar.filterListView: <UIScrollView: 0x8489f90; frame = (0 0; 768 934); clipsToBounds = YES; alpha = 0.8; gestureRecognizers = <NSArray: 0x848a650>; animations = { opacity=<CABasicAnimation: 0x98138b0>; }; layer = <CALayer: 0x848a160>; contentOffset: {0, 0}>
2013-02-22 16:28:22.913 [5196:14003] self.datasetSubBar.panels: (
    "<DatasetFilterListPanelView: 0x9853570; frame = (-385 20; 365 165); tag = 1; layer = <CALayer: 0x9853640>>"

这感觉就像在子视图的创建的时候,filterListView的宽度为0的原因,如果上海华是768,子视图锚应为(383,20){上海华的宽度(768) - 子视图的宽度(365) - 填充(20)}

It feels like at the time of the subView's creation, the filterListView's width is 0 cause if the superView was 768, the subView anchor should be (383, 20) {superView's width (768) - subView's width(365) - padding(20)}.

这是令人沮丧的我,它应该工作。做UIScrollViews有约束的行为不同?

This is frustrating me, it should work. Do UIScrollViews act differently with constraints?

编辑:

是的,UIScrolView处理的限制不同;我改变了滚动视图一个UIView和作品像它应该。

Yes, UIScrolView handles constraints differently; I changed the scroll view to a UIView and works like it should.

推荐答案

做了更多挖掘和想通了,我需要在滚动视图使用内容查看:

Did more digging and figured out I would need to use a contentView in the scroll view:

-(UIScrollView *)createFilterListView {    
    UIScrollView *scrollView = [UIScrollView new];
    scrollView.translatesAutoresizingMaskIntoConstraints = FALSE;

    UIView *contentView = [UIView new];

    scrollView.contentView = contentView;

    [scrollView addSubview:contentView];

    return scrollView;
}

又在哪里滚动视图实现,设置约束它,然后设定的内容查看和contentSize你需要的大小:

Then where the scroll view is implemented, set the constraints for it then set the contentView and contentSize the size you need:

[self.view layoutIfNeeded];

self.scrollView.contentView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.width * 2);

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.width * 2); // made it twice as big to test scrolling

我添加了一个类别的UIScrollView存储内容视图。然后,使用约束上添加项目到内容查看。当你的项目需要滚动,您需要调整内容查看和contentSize。

I added a category to UIScrollView to store the content view. You then add the items to the contentView using contraints. When your items need to scroll, you will need to resize the contentView and contentSize.

的iOS 6发行说明去了这一点。

这篇关于的UIScrollView和约束的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆