gpt4 book ai didi

ios - UIScrollView 和约束问题

转载 作者:行者123 更新时间:2023-11-29 04:07:00 25 4
gpt4 key购买 nike

我创建一个 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 中,添加 ScrollView 并为其指定大小:

[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 以放置在 ScrollView 中:

[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 anchor 应该是 (383, 20) {superView 的宽度 (768) - subView 的宽度(365) - padding (20)}。

这让我很沮丧,它应该有效。 UIScrollViews 在约束条件下的行为是否有所不同?

编辑:

是的,UIScrolView 以不同的方式处理约束;我将 ScrollView 更改为 UIView 并按其应有的方式工作。

最佳答案

做了更多挖掘并发现我需要在 ScrollView 中使用 contentView:

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

UIView *contentView = [UIView new];

scrollView.contentView = contentView;

[scrollView addSubview:contentView];

return scrollView;
}

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

iOS 6 release notes也回顾一下这一点。

关于ios - UIScrollView 和约束问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15034413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com