iOS 在XIB上的View添加子视图的布局优化

一、在XIB上的View上用addSubview方法添加View1的时候,注意要用上自动布局,这样当这个View布局改变的时候,View1也会自适应改变布局;

    [self.annularView removeFromSuperview];
    self.annularView = nil;
    XRAnnularPieView *annularView = [[XRAnnularPieView alloc] initWithFrame:self.wrongQuetionChartBgView.bounds]; // 这个frame还是获取的上次的,不是最新的布局;
    self.annularView = annularView;
    annularView.valueArray = sortValueArr;
    annularView.colorArray = sortColorArr;
    annularView.showAnimation = NO;
    annularView.lineWidth = 10.f;
    // annularView.radius = self.wrongQuetionChartBgView.width/2 - 8;
    //[annularView strokePath];
    //annularView.center = CGPointMake(self.wrongQuetionChartBgView.width/2, self.wrongQuetionChartBgView.width/2);
    [self.wrongQuetionChartBgView addSubview:annularView];
    [annularView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];// 用自动布局,圆环View是跟圆环背景一样大了;但是圆环没跟着改变啊!

二、但是,这个View1上的子视图的布局却不会跟着改变,因为用的是frame布局的,这个时候View1的子视图是绘制了一个圆环,所以要在View1的layoutSubviews上重新绘制子视图;

XRAnnularPieView

-(void)layoutSubviews {
    [super layoutSubviews];
    NSLog(@"圆环背景 --- XRAnnularPieView本身 --- %@",NSStringFromCGRect(self.frame));
    self.radius = self.width/2 - 2;
    self.centerPoint = CGPointMake(self.width/2, self.height/2);
    [self strokePath];
}

猜你喜欢

转载自blog.csdn.net/qq_27247497/article/details/114976844