将动态创建的UIView集中在故事板中的UILabel上不起作用

时间:2015-04-14 17:35:16

标签: ios objective-c uiview uiviewcontroller storyboard

我有一个UILabel放在我的故事板上,带有AutoLayout。 (约束包括高度宽度水平居中,以及从顶部的垂直间距)。

UIView使用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 ,这给了我视图看看圈子。

enter image description here

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

通过Photoshop的魔力,这就是我想要实现的目标: enter image description here

3 个答案:

答案 0 :(得分:2)

然后你做错了什么......使用约束!

这是我的故事板标签约束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

一块蛋糕! ;)

答案 1 :(得分:0)

这就是我要做的事情:

  1. 确保没有可能推动圈子的限制
  2. 让圈子视图不是UILabel的子视图,而是superview的子视图(你可能已经这样做了)
  3. 将圆形视图的框架设置为

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

答案 2 :(得分:0)

viewDidAppear而不是viewDidLoad

中执行圈子的初始化