[英]Center a dynamically created UIView over a UILabel in the storyboard not working
我有一个带有自动布局的UILabel
放置在我的情节提要上。 (约束包括高度 , 宽度和水平居中以及距顶部的垂直距离 )。
我有UIView
使用UIBeizerPath
与drawRect
方法UIBeizerPath
。
到目前为止还算不上什么。
为了简单起见,我使用硬编码数字来帮助说明我的问题。
现在,我希望将CircleView放置在UILabel
,将标签在圆中居中。
但是,我什么也无法正确排队。 它像锚点或混乱的东西。
我尝试将CircleView的中心点设置为标签的中心点。 没运气。
我尝试将CircleViews X设置为标签的位置减去宽度除以2的位置。 没运气。
我能得到的最接近的是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。 (半径x 2)+ 5 ,这给了我用圆看到的视图。
深色背景是iOS模拟器的视图。 我为每个元素添加了背景色以区分它们的大小。
通过Photoshop的魔力,这就是我想要实现的目标:
那就说明您做错了...使用约束!
这是我的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
这就是结果
小菜一碟 ! ;)
这就是我要做的:
将圆形视图的框架设置为
CGRectMake(self.myLabel.frame.origin.x - (155 / 2.0), self.myLabel.frame.origin.y - (155 / 2.0), 155, 155);
在viewDidAppear
而不是viewDidLoad
执行圆形的初始化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.