찾다

 >  Q&A  >  본문

ios - 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];
}
高洛峰高洛峰2772일 전1283

모든 응답(2)나는 대답할 것이다

  • 天蓬老师

    天蓬老师2017-04-17 17:14:57

    _containerView를 지울 때 _lastBtn을 비우는 것을 잊었습니다. 결과적으로 _lastBtn은 비어 있지 않지만 버튼은 실제로 뷰 계층 구조에 없습니다

    회신하다
    0
  • 怪我咯

    怪我咯2017-04-17 17:14:57

    '공통 슈퍼뷰를 찾을 수 없습니다. 이 오류가 발생하는 이유는 제약 조건을 설정한 컨트롤과 이것이 종속된 컨트롤에 공통 슈퍼뷰가 없기 때문입니다. 참조할 공통 뷰가 없기 때문에 프레임을 동일한 좌표계로 변환할 수 없습니다. 이 문제는 상위 컨트롤에 추가하지 않고 제약 조건을 설정하기 위한 뷰를 생성할 때 또는 제약 조건을 설정하기 위한 뷰와 그것이 의존하는 뷰에 공통 상위 뷰가 없을 때, 즉 이러한 상황이 발생하는 경우에 자주 발생합니다.

    회신하다
    0
  • 취소회신하다