【问题标题】:UIScrollView and constraints issueUIScrollView 和约束问题
【发布时间】:2013-02-22 22:39:51
【问题描述】:

我创建了一个 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 并正常工作。

【问题讨论】:

    标签: ios objective-c autolayout nslayoutconstraint


    【解决方案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 也检查一下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-25
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 2019-03-28
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多