将动态创建的 UIView 置于情节提要中的 UILabel 的中心无法正常工作

Center a dynamically created UIView over a UILabel in the storyboard not working

我有一个 UILabel 使用 AutoLayout 放置在我的 Storyboard 上。 (约束包括高度宽度水平居中,以及垂直间距顶部).

我有 UIView 使用 UIBeizerPath 通过 drawRect 方法制作圆圈。

到目前为止没什么特别的。

为了简单起见,我使用硬编码数字来帮助说明我的问题。

现在我希望将 CircleView 放置在 UILabel 上,使标签在圆中居中。

但是,我无法正确排列任何东西。它就像锚点或什么东西搞砸了。

我尝试将 CircleView 的中心点设置为标签中心点。运气不好。

我尝试将 CircleViews X 设置为标签位置减去宽度除以二。运气不好。

我能得到的最接近的是 Y 坐标。我使用 label.center.y - 52.5(半径的一半)。

cv = [[CircleView alloc] initWithFrame:CGRectMake(WHATHERE.x, WHATHERE.y,
                                                      155, 155)];
cv.radius           = 75;
cv.strokeWidth      = 5;

圆的半径为75。CircleView的width/height为155,因为圆的行程为5。(半径x 2) + 5 , 这给了我你用圆圈看到的视图。

深​​色背景是iOS模拟器的视图。我为每个元素添加了背景颜色以区分它们的大小。

借助 Photoshop 的魔力,我正在努力实现以下目标:

我会这样做:

  1. 确保没有可能推动圆圈的约束
  2. 使圆形视图不是 UILabel 的子视图,而是超级视图的子视图(您可能已经这样做了)
  3. 将圆形视图的框架设置为

    CGRectMake(self.myLabel.frame.origin.x - (155 / 2.0), self.myLabel.frame.origin.y - (155 / 2.0), 155, 155);
    

viewDidAppear而不是viewDidLoad

中执行圆的初始化

那你做错了……使用约束!

这是我的 Storyboard 标签约束

这是我的ViewController

    @interface ViewController ()

    @property (nonatomic, weak) IBOutlet UILabel *centeredLabel;

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.

        CircleView *circleView = [CircleView new];
        [self.view addSubview:circleView];

        circleView.translatesAutoresizingMaskIntoConstraints = NO;

        NSLayoutConstraint *circleCenterX = [NSLayoutConstraint constraintWithItem:circleView
                                                                         attribute:NSLayoutAttributeCenterX
                                                                         relatedBy:NSLayoutRelationEqual
                                                                            toItem:self.centeredLabel
                                                                         attribute:NSLayoutAttributeCenterX
                                                                        multiplier:1 constant:0];


        NSLayoutConstraint *circleCenterY = [NSLayoutConstraint constraintWithItem:circleView
                                                                         attribute:NSLayoutAttributeCenterY
                                                                         relatedBy:NSLayoutRelationEqual
                                                                            toItem:self.centeredLabel
                                                                         attribute:NSLayoutAttributeCenterY
                                                                        multiplier:1 constant:0];
        CGFloat circleDiameter = 155;

        NSLayoutConstraint *circleWidth = [NSLayoutConstraint constraintWithItem:circleView
                                                                         attribute:NSLayoutAttributeWidth
                                                                         relatedBy:NSLayoutRelationEqual
                                                                            toItem:nil
                                                                         attribute:NSLayoutAttributeNotAnAttribute
                                                                        multiplier:1 constant:circleDiameter];
        NSLayoutConstraint *circleHeight = [NSLayoutConstraint constraintWithItem:circleView
                                                                         attribute:NSLayoutAttributeHeight
                                                                         relatedBy:NSLayoutRelationEqual
                                                                            toItem:circleView
                                                                         attribute:NSLayoutAttributeWidth
                                                                        multiplier:1 constant:0];

        [self.view addConstraints:@[circleCenterX, circleCenterY, circleWidth, circleHeight]];
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    @end

这是 CircleView

    @implementation CircleView

- (instancetype)init{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    UIColor *blueTransparent = [[UIColor blueColor] colorWithAlphaComponent:0.4];
    [blueTransparent setFill];
    CGContextFillRect(ctx, self.bounds);
    UIColor *circleColor = [UIColor greenColor];
    [circleColor setStroke];

    CGContextSetLineWidth(ctx, 6);
    CGContextStrokeEllipseInRect(ctx, self.bounds);

}

@end

这是结果

小菜一碟! ;)