UIScrollView and constraints issue

微笑、不失礼 提交于 2019-12-12 03:49:12

问题


I create a 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;

Then in my UIViewController, I add the scroll view and give it a size:

[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

I then create a custom UIView to place in the scroll view:

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

It should place the subView in the top right corner, but it doesn't:

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

It feels like at the time of the subView's creation, the filterListView's width is 0 cause if the superView was 768, the subView anchor should be (383, 20) {superView's width (768) - subView's width(365) - padding(20)}.

This is frustrating me, it should work. Do UIScrollViews act differently with constraints?

EDIT:

Yes, UIScrolView handles constraints differently; I changed the scroll view to a UIView and works like it should.


回答1:


Did more digging and figured out I would need to use a contentView in the scroll view:

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

    UIView *contentView = [UIView new];

    scrollView.contentView = contentView;

    [scrollView addSubview:contentView];

    return scrollView;
}

Then where the scroll view is implemented, set the constraints for it then set the contentView and contentSize the size you need:

[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

I added a category to UIScrollView to store the content view. You then add the items to the contentView using contraints. When your items need to scroll, you will need to resize the contentView and contentSize.

iOS 6 release notes go over this as well.



来源:https://stackoverflow.com/questions/15034413/uiscrollview-and-constraints-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!