ScrollView不是子视图,不与autolayout交互

时间:2015-03-29 09:03:36

标签: objective-c uiscrollview autolayout

我已经建立了一个" StackView"所以在滚动视图上添加视图会更容易一些,看起来像这样:

@implementation StackViewAutoLayout {
    UIView *lastView;
    UIView *contentView;
    double contentHight;
}

-(void) setupContentView {

    contentView = [[UIView alloc] init];
    contentView.userInteractionEnabled = YES;
    [self addSubview:contentView];

    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];

    contentHight = 0;
}

- (void)addSubviewWithLayout:(UIView *)view {

    if (!contentView) {

        [self setupContentView];
    }

    [contentView addSubview:view];

        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lastView ? lastView.mas_bottom : @0);
            make.left.equalTo(@0);
            make.width.equalTo(@([UIScreen mainScreen].bounds.size.width));
            make.height.equalTo(@(view.height));
        }];


    view.userInteractionEnabled = YES;
        lastView = view;

    contentHight +=  view.height;

    [contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(contentHight));
    }];

}

我这样使用它:

self.overViewScroll = [StackViewAutoLayout new];

[self addSubview:self.overViewScroll];

[self.overViewScroll mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.mas_left);
            make.right.equalTo(self.mas_right);
            make.top.equalTo(self.headerViewModel.view.mas_bottom);
            make.bottom.equalTo(self.mas_bottom);
}];

[self.overViewScroll addSubviewWithLayout:self.stockDetailsViewModel.view];
[self.overViewScroll addSubviewWithLayout:self.chartViewModel.view];
[self.overViewScroll addSubviewWithLayout:self.summeryViewModel.view];
[self.overViewScroll addSubviewWithLayout:self.postSomthing];

[self setViewStateAccourdingToSwitch];

布局很漂亮,我正在寻找。 问题是,添加的视图的尼姑不是使用教学!

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:0)

找到一种方法

@implementation StackViewAutoLayout {
    UIView *lastView;
    double contentHight;
}

-(void) setupContentView {
    contentHight = 0;

    [self setDelaysContentTouches:NO];
    self.canCancelContentTouches = NO;
}

- (void)addSubviewWithLayout:(UIView *)view {

    [self addSubview:view];

        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lastView ? lastView.mas_bottom : @0);
            make.left.equalTo(@0);
            make.width.equalTo(@([UIScreen mainScreen].bounds.size.width));
            make.height.equalTo(@(view.height));
        }];


    view.userInteractionEnabled = YES;
        lastView = view;

    contentHight +=  view.height;

    self.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, contentHight);

}