UIScrollView和约束问题

时间:2013-02-22 22:39:51

标签: ios objective-c autolayout nslayoutconstraint

我创建了一个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中,我添加滚动视图并给它一个大小:

[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放置在滚动视图中:

[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];
}

它应该将subView放在右上角,但它不会:

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>>"

感觉就像在创建subView时,filterListView的宽度是0,如果superView是768,则subView锚点应该是(383,20){superView的宽度(768) - subView的宽度(365) - 填充(20)}。

这令我感到沮丧,它应该有效。 UIScrollViews对约束的行为有何不同?

编辑:

是的,UIScrolView处理不同的约束;我将滚动视图更改为UIView并按原样运行。

1 个答案:

答案 0 :(得分:1)

进行了更多挖掘并想出我需要在滚动视图中使用contentView:

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

    UIView *contentView = [UIView new];

    scrollView.contentView = contentView;

    [scrollView addSubview:contentView];

    return scrollView;
}

然后在实现滚动视图的地方,为它设置约束,然后将contentView和contentSize设置为您需要的大小:

[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中添加了一个类别来存储内容视图。然后使用约束将项目添加到contentView。当您的项目需要滚动时,您需要调整contentView和contentSize的大小。

iOS 6 release notes也可以了解这一点。