IOS adds layout optimization of subviews to View on XIB

1. When using the addSubview method to add View1 to the View on XIB, pay attention to the use of automatic layout, so that when the View layout changes, View1 will also change the layout adaptively;

    [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是跟圆环背景一样大了;但是圆环没跟着改变啊!

2. However, the layout of the subview on View1 will not change accordingly, because the frame layout is used. At this time, the subview of View1 draws a circle, so the subview should be redrawn on the layoutSubviews of View1. ;

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];
}

Guess you like

Origin blog.csdn.net/qq_27247497/article/details/114976844