Created
March 6, 2014 12:22
-
-
Save edom18/9388453 to your computer and use it in GitHub Desktop.
[Objective-C] UIViewをいい感じに上下左右センタリングする ref: http://qiita.com/edo_m18/items/5dd48b7ee1e6899b8ed5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ベースのビュー(これをセンタリングする) | |
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