我创建了一个 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) - padding(20) }。
这让我很沮丧,它应该工作。UIScrollViews 对约束的行为是否不同?
编辑:
是的,UIScrollView 以不同的方式处理约束;我将滚动视图更改为 UIView 并按其应有的方式工作。