如何将UILabel放置在UIImageView下方,使其始终均匀放置

时间:2016-08-29 07:00:25

标签: ios objective-c uiimageview uilabel cgrectmake

我制作了一个自定义弹出视图,看起来像这样

我想在用户图片下方放置用户名标签,该图片是给定图片中的舍入UIImageView

我希望无论文本大小如何,都要均匀放置用户名。 就像我希望名称看起来像这样

enter image description here

3 个答案:

答案 0 :(得分:1)

  1. 将标签内容与中心对齐。

  2. 设置从标签到图像视图的约束。将标签水平居中到图像视图。

  3. Align label horizontally to imageview

答案 1 :(得分:0)

As you are not using the storyboard, Below code will help you in setting the frame of label:-

UILabel *labelName;//This will be your label instance
CGFloat yAxis = 50;//This will be your image view's y axis + image view's height
[labelName setFrame:CGRectMake(0, yAxis, self.view.frame.size.width, self.view.frame.size.height)];
//Set the color/Font other properties of label as desired

[labelName setTextAlignment:NSTextAlignmentCenter];

答案 2 :(得分:0)

设置约束是有效的解决方案。

要么你可以按照Mr.UB建议的故事板方式,要么按照代码的方式进行处理。如果你想通过代码继续,我建议你使用Masonry来设置约束。

你可以这样做:

[your-label mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(imgView.mas_top); //with is an optional semantic filler
    make.mas_centerX.equalTo(imgView.mas_centerX);
}];