首页 > masonry布局 couldn't find a common superview错误

masonry布局 couldn't find a common superview错误

写的一个scrollView里循环滚动新闻的控件,在updateForTopTalkArray方法中添加

if ([_containerView subviews].count) {
        for (UIView *view in [_containerView subviews]) {
            if ([view isKindOfClass:[DyTopTalkButton class]]) {
                [view removeFromSuperview];
            }
        }
    }

变回报错,下面是错误信息:

2015-11-28 17:42:51.793 WanFanTian[9068:2279758] *** Assertion failure in -[MASViewConstraint install], /Users/deyi/Documents/Deyi/WanFanTian/Pods/Masonry/Masonry/MASViewConstraint.m:345
2015-11-28 17:42:51.966 WanFanTian[9068:2279758] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'couldn't find a common superview for <DyTopTalkButton: 0x7fe2f84b3330; baseClass = UIButton; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x7fe2fa85b2e0>> and <DyTopTalkButton: 0x7fe2fa988a50; baseClass = UIButton; frame = (0 144; 304 36); opaque = NO; layer = <CALayer: 0x7fe2fa987ee0>>'

其中MASViewConstraint.m:345的内容就是:

 @"couldn't find a common superview for %@ and %@",

同时按钮添加的监听方法无效,点击之后没有效果
望大牛能够帮忙解决!!!

详细代码如下:

#import "DySelectionTopTalkCell.h"
#import "WebViewController.h"
static const CGFloat cellHeight = 36.f;
@implementation DySelectionTopTalkCell{
    DyTopTalkButton *_lastBtn;
    NSTimer *timer;
    NSInteger currentPage;
}

- (void)initView{
    [super initView];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor whiteColor];
    _topLineView = [[UIView alloc]init];
    _bottomLineView = [[UIView alloc]init];
    _seperatorLineView = [[UIView alloc]init];
    _topLineView.backgroundColor = [UIColor appSplitLineColor];
    _bottomLineView.backgroundColor = [UIColor appSplitLineColor];
    _seperatorLineView.backgroundColor = [UIColor appSplitLineColor];
    _label = [[UILabel alloc]init];
    [_label setTextColor:[UIColor appAirConditionOrangeColor]];
    [_label setFont:[UIFont systemFontOfSize:13]];
    NSString *text = @"今日头条";
    [_label setText:text];
    CGSize labelSize = [text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(30, 36)];
    NSLog(@"labe-大小%@",NSStringFromCGSize(labelSize));
    [_label setFrame:CGRectMake(0, 0, labelSize.width, labelSize.height)];
    _topTalkScrollView = [[UIScrollView alloc]init];
    _topTalkScrollView.bounces = NO;
    _topTalkScrollView.pagingEnabled = YES;
    _topTalkScrollView.scrollEnabled = NO;
    _containerView = [[UIView alloc]init];

    [self.contentView addSubview:_topTalkScrollView];
    [_topTalkScrollView addSubview:_containerView];

    [self.contentView addSubview:_topLineView];
    [self.contentView addSubview:_bottomLineView];
    [self.contentView addSubview:_seperatorLineView];
    [self.contentView addSubview:_label];
        timer = [NSTimer timerWithTimeInterval:2.f target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
    [self layoutConstraints];
    
}

- (void)updateTimer{
    if (!_top_talkArray.count) {
        return;
    }
        if (currentPage == _top_talkArray.count-1) {
            currentPage = 0;

             [_topTalkScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        }else{
        currentPage ++;
        [UIView animateWithDuration:0.6f animations:^{
            [_topTalkScrollView setContentOffset:CGPointMake(0, currentPage*cellHeight) animated:YES];
        }];
        }
    }




- (void)updateForTopTalkArray{

    if ([_containerView subviews].count) {
        for (UIView *view in [_containerView subviews]) {
            if ([view isKindOfClass:[DyTopTalkButton class]]) {
                [view removeFromSuperview];
            }
        }
    }
    NSString *url = nil;
       if (_top_talkArray.count) {
        for (NSDictionary *dataDic in _top_talkArray) {
            DyTopTalkButton *scrollButton = [[DyTopTalkButton alloc]init];
            scrollButton.title = dataDic[@"title"];
            scrollButton.type = [dataDic[@"type"]integerValue];
//            scrollButton.backgroundColor =[UIColor colorWithHue:( arc4random() % 256 / 256.0 )
//                                                     saturation:( arc4random() % 128 / 256.0 ) + 0.5
//                                                     brightness:( arc4random() % 128 / 256.0 ) + 0.5
//                                                          alpha:1];
            url = dataDic[@"url"];
            if(url) {
                scrollButton.url = url;
                [scrollButton setTitleColor:[UIColor appAirConditionOrangeColor] forState:UIControlStateNormal];
//                [scrollButton addTarget:self action:@selector(topTalkbtnClick:) forControlEvents:UIControlEventTouchUpInside];
                [scrollButton addTarget:self action:@selector(topTalkbtnClick:) forControlEvents:UIControlEventTouchDragInside];
                NSLog(@"%@--网址",url);
            }
            [_containerView addSubview:scrollButton];
            [scrollButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(_containerView);
                make.height.mas_equalTo(cellHeight);
                if (_lastBtn) {
                    make.top.equalTo(_lastBtn.mas_bottom);
                }else{
                    make.top.equalTo(_containerView.mas_top);
                }
            }];
            _lastBtn = scrollButton;
        }
    }
    else{
        DyTopTalkButton *scrollButton = [[DyTopTalkButton alloc]initWithFrame:_containerView.bounds];
        scrollButton.title = @"小编睡懒觉了,尝试刷新一下叫醒她!";
        [_containerView addSubview:scrollButton];
        [scrollButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(_containerView);
            make.height.mas_equalTo(cellHeight);
        }];
    }
//
//    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(_lastBtn.mas_bottom);
//    }];

    }



- (void)layoutConstraints{
    WeakSelf(weakSelf);

    [_topTalkScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_seperatorLineView.mas_right).offset(4.f);
        make.top.bottom.right.equalTo(weakSelf.contentView);
//        make.height.mas_equalTo(cellHeight);
//        NSLog(@"-----scroll布局属性---%@",NSStringFromCGRect(_topTalkScrollView.frame));
    }];

    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(_topTalkScrollView);
        make.width.equalTo(_topTalkScrollView);
        
    }];
    [_topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView);
        make.left.equalTo(weakSelf.contentView);
        make.width.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1.f);
    }];
    [_bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView);
        make.bottom.width.equalTo(weakSelf.contentView);
        make.width.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1.f);
    }];

    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(7.f);
        make.centerY.equalTo(weakSelf.contentView);
        make.width.mas_equalTo(55);
    }];

    [_seperatorLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1.f);
        make.top.equalTo(weakSelf.contentView).offset(7.f);
        make.bottom.equalTo(weakSelf.contentView).offset(-7.f);
        make.left.equalTo(_label.mas_right).offset(4.f);
    }];

}


- (void)topTalkbtnClick:(DyTopTalkButton *)btn{
    NSLog(@"clicked 点击");
    WebViewController *web = [[WebViewController alloc]initWithWeburl:btn.url];
    web.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:web animated:YES];
}

'couldn't find a common superview for 出现这个错误的原因是,你所设置约束的这个控件和所依赖的控件没有共同的父视图。因为没有共同的视图作为参照,frame 就不能转换到一个相同的坐标系。这个问题经常会出现在,我们创建了要设置约束的视图,而没有将它添加到父控件中,又或者,要设置约束的这个视图和所依赖的视图没有共同的父视图,也就是你遇到的这种情况。


你在清空_containerView的时候忘记对_lastBtn进行置空了吧。导致_lastBtn非空,但这个button实际上不在view hierarchy上

【热门文章】
【热门文章】