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];
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值