ios - masonry布局 couldn't find a common superview错误
高洛峰
高洛峰 2017-04-17 17:12:57
0
2
1266

写的一个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];
}
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
Peter_Zhu

You forgot to empty _lastBtn when clearing _containerView. As a result, _lastBtn is not empty, but the button is not actually in the view hierarchy

刘奇

'couldn't find a common superview for The reason for this error is that the control you set the constraints and the control it depends on do not have a common superview. Because there is no common view as a reference, the frames cannot be transformed to the same coordinate system. This problem often occurs when we create the view to set constraints without adding it to the parent control, or the view to set constraints and the view it depends on do not have a common parent view, that is, you encounter to this situation.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!