3

iPhone 6 Plus 対応のアプリを更新中です。プログラムで設定されたビューの 1 つで、5S 以下で実行すると、すべてのビューが本来あるべき中央に配置されます。ただし、6 Plusでは、わずかに左にスクートされています. このコードを調整して、すべてのバージョンで水平方向の中央に配置するにはどうすればよいですか?

[self.logInView.usernameField setFrame:CGRectMake(35.0f, 125.0f, 250.0f, 50.0f)];
[self.logInView.passwordField setFrame:CGRectMake(35.0f, 175.0f, 250.0f, 50.0f)];
[self.fieldsBackground setFrame:CGRectMake(35.0f, 125.0f, 250.0f, 100.0f)];
[self.logInView.logInButton setFrame:CGRectMake(35.0f, 235.0f, 250.0f, 40.0f)];
4

4 に答える 4

0

答えは簡単です...自動レイアウトを使用してください。ただし、使い方がわからない場合は、次の方法を試してください。

最初に、いくつかの定義を作成します。

#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPADDEVICE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE_4 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 480.0)
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
#define IS_IPAD (IS_IPADDEVICE && [[UIScreen mainScreen] bounds].size.height == 1024.0)

次に、単純な if ステートメントを作成します。

   if (IS_IPHONE_4) {
        // iPhone 4
    } else if (IS_IPHONE_5) {
        // iPhone 5
    } else if (IS_IPHONE_6) {
        // iPhone 6
    } else if (IS_IPHONE_6_PLUS) {
        // iPhone 6 plus
    } else if (IS_IPAD) {
        // iPad
    } else {
        // other device
    }

オブジェクトを中央に配置したい場合は、次のframeメソッドを使用します。

theobject.frame = CGRectMake((theobject.frame.size.width/2) - (theobject.image.size.width/2), 20, theobject.image.size.width, theobject.image.size.height);

この回答がお役に立てば幸いです。;)

于 2015-01-11T04:53:28.387 に答える
-3

「境界」と「中心」を使用できます。

self.logInView.usernameField.bounds = CGRectMake(0,0,250,50);
self.logInView.usernameField.center = CGPointMake(160, 150);
于 2015-01-11T04:31:56.767 に答える