我創建一個UIScrollView:的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>>"
感覺就像在子視圖的創建時,所述filterListView的寬度爲0的原因,如果上海華是768,子視圖錨應爲(383,20){上海華的寬度(768) - 子視圖的寬度( 365) - 填充(20)}。
這令我很沮喪,它應該工作。 UIScrollViews的行爲與約束不同嗎?
編輯:
是的,UIScrolView處理約束不同;我將滾動視圖更改爲一個UIView,並像它應該那樣工作。