【发布时间】:2014-02-24 14:49:02
【问题描述】:
我正在尝试使用图像和数字创建 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 设置为按内容动态更改宽度?谢谢
【问题讨论】:
-
你可以使用自动布局吗?
-
我猜是这样,它必须只是在代码中(以编程方式)。
标签: ios objective-c uiview custom-controls