LoginSignup
9
8

More than 3 years have passed since last update.

[iOS8 対応]UILabelをbackgroundColorを追加後、addSubVievするとViewが隠れる問題

Last updated at Posted at 2014-09-11

既存のアプリXcode6で実行した

UILabel *label1 = [[UILabel alloc]initWithFrame:self.view.frame];
label1.backgroundColor = [UIColor whitecolor];

UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0,0,100,100)];
label2.backgroundColor = [UIColor redcolor];

[label1 addSubView:label2];
[self addSubview:label1];

label1のみが表示されるように見えるが、実際は、backgroundColorの色で塗りつぶされていた。

対応策1

UILabel *label1 = [[UILabel alloc]initWithFrame:self.view.frame];
label1.backgroundColor = [UIColor whitecolor];

UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0,0,100,100)];
label2.backgroundColor = [UIColor redcolor];

[label1 insertSubView:label2 atIndex:0];
[self addSubview:label1];

変わらず

対応策2

UILabel *label1 = [[UILabel alloc]initWithFrame:self.view.frame];
label1.backgroundColor = [UIColor whitecolor];

UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0,0,100,100)];
label2.backgroundColor = [UIColor redcolor];

[label1.superview insertSubview:label2 belowSubview:blabel];
[self addSubview:label1];

blabel表示されず

対応策3

UILabel *label1 = [[UILabel alloc]initWithFrame:self.view.frame];
label1.layer.backgroundColor = [UIColor whitecolor].CGColor;

UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0,0,100,100)];
label2.backgroundColor = [UIColor redcolor];

[label1 addSubView:label2];
[self addSubview:label1];

ios7と同じ見た目になった。

あとがき

UILabelの挙動が変わっていて、つまずきました。
新規でProjectを作成すると、今までどおりで表示されていたので、意味不明でした。
同じ症状の人の為に、メモしておきます。

くわしい方は、ぜひ原因を教えてください。

9
8
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8