我想创建具有图像和编号的UIView。我想集中他们。现在我有这样的代码:iOS - UIView的中心内容
CGRect likesRect = CGRectMake(0,152, screenWidth/2, 30);
MARoundedRect *likesRoundedRect = [[MARoundedRect alloc] initWithFrame:likesRect];
UIView *likesView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
likesView.center = CGPointMake(screenWidth/4, 20);
UIImageView *likeImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
likeImage.image = [UIImage imageNamed:@"favorite.png"];
UILabel *likesLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, screenWidth/2, 20)];
[likesLabel setTextColor:[UIColor whiteColor]];
[likesLabel setBackgroundColor: [UIColor clearColor]];
[likesLabel setText:[actualPlace.Favorites stringValue]];
[likesView addSubview:likeImage];
[likesView addSubview:likesLabel];
[likesRoundedRect addSubview:likesView];
[viewController.view addSubview:likesRoundedRect];
现在它的工作不错,但我设置likesView的宽度精确值,我想这变坏的时候,我没有数与一个或两个字母,但更多。我怎样才能解决这个问题?如果两个或多个组件并排存在,如何集中UIView的内容?或者我如何设置UIView动态改变宽度的内容?由于
您能使用自动布局? – Wain
我想是的,它只能在代码中(以编程方式)。 –