Skip to content

Instantly share code, notes, and snippets.

@edom18
Created March 6, 2014 12:22
Show Gist options
  • Save edom18/9388453 to your computer and use it in GitHub Desktop.
Save edom18/9388453 to your computer and use it in GitHub Desktop.
[Objective-C] UIViewをいい感じに上下左右センタリングする ref: http://qiita.com/edo_m18/items/5dd48b7ee1e6899b8ed5
// ベースのビュー(これをセンタリングする)
UIView *baseView = [[UIView alloc] init];
[baseView addSubview:label];
// アイコンビュー
UIImage image = [UIImage imageNamed:@"hoge"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[baseView addSubview:imageView];
// アイコンのラベル
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame), 0, 0)];
label.text = @"test";
[baseView addSubview:label];
[label sizeToFit]; // テキストにフィットさせる
// アイコン+テキストの領域を内包するCGRectを生成
baseView.frame = CGRectUnion(label.frame, imageView.frame);
baseView.center = CGPointMake(self.view.bounds.width / 2, self.view.bounds.height / 2);
imageView.center = CGPointMake(baseView.bounds.width / 2, imageView.bounds.size.height / 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment