0

アプリの UILabel を作成するための簡単なコードは次のとおりです。

UILabel *todaysGame = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 300, 250)];
todaysGame.text = @"Today's Game";
todaysGame.backgroundColor = [UIColor grayColor];
[self.view addSubview:todaysGame];

これは私の iPhone 5 の画面の高さにぴったりですが、iPhone 4 の画面では台無しになりました。iOS 6 の自動レイアウト機能を読んでみましたが、やり方がよくわかりませんでした。ストーリーボードや NIB を使用したくありません。プログラムで実行したいと考えています。iPhone 4 および 5 の画面の高さと互換性のある UI 要素を配置する方法。

また、数字を使用する代わりにこれが役立つかどうかも確認しようとしました

screenBound = [[UIScreen mainScreen] bounds];
screenSize = screenBound.size;
screenWidth = screenSize.width;
screenHeight = screenSize.height;

「screenHeight」変数を CGRectMake() メソッドに使用しました。

Xcode 4.6 と iPhone 5 (iOS 6.1.3) と iPhone 4 を同じものに使用しています。

4

2 に答える 2

0

あなたの望む効果は何ですか?同じ高さ、可変高さ、上余白など?

ラベルを常にそのサイズにしますか? その場合は、自動サイズ変更をオフにすることができます

label.translatesAutoresizingMaskIntoConstraints = NO;

ラベルをビューと同じ高さにしたい場合...

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0];
[self.view addConstraint:constraint];
于 2013-04-14T15:09:19.880 に答える