我想創建具有圖像和編號的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
我想是的,它只能在代碼中(以編程方式)。 –