gpt4 book ai didi

ios - 将动态创建的 UIView 置于 Storyboard 中的 UILabel 的中心无法正常工作

转载 作者:行者123 更新时间:2023-11-28 21:47:15 25 4
gpt4 key购买 nike

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

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

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

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

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

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

我尝试将 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 的宽度/高度是 155,因为圆的行程是 5。(radius x 2) + 5,这让我看到了你看圆圈。

enter image description here

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

通过 Photoshop 的魔力,我正在努力实现以下目标: enter image description here

最佳答案

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

这是我的 Storyboard 标签约束 enter image description here

这是我的 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

这是结果

enter image description here

小菜一碟! ;)

关于ios - 将动态创建的 UIView 置于 Storyboard 中的 UILabel 的中心无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29633881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com