14

私は12秒のView Controllerを持っていますUITextField

3.5インチのディスプレイにぴったりです。

ここに画像の説明を入力

iPhone 5 (4 インチ ディスプレイ)では、間に余分なスペースを追加してすべてUITextFieldが全体を覆うように設定する必要があります。UIView

自動レイアウトでこれをやろうとしていますが、うまくいきません。

ここに画像の説明を入力

これは私のコードです:

- (void) viewWillLayoutSubviews
{
    int h = txt1.bounds.size.height * 12;

    float unusedHorizontalSpace = self.view.bounds.size.height - h ;

    NSNumber* spaceBetweenEachButton=  [NSNumber numberWithFloat: unusedHorizontalSpace / 13 ] ;

    NSMutableArray *constraintsForButtons = [[NSMutableArray alloc] init];

    [constraintsForButtons addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|-50-[txt1(==30)]-(space)-[txt2(==txt1)]-(space)-[txt3(==txt1)]-(space)-[txt4(==txt1)]-(space)-[txt5(==txt1)]-(space)-[txt6(==txt1)]-(space)-[txt7(==txt1)]-(space)-[txt8(==txt1)]-(space)-[txt9(==txt1)]-(space)-[txt10(==txt1)]-(space)-[txt11(==txt1)]-(space)-[txt12]-(space)-|"
                                                                                        options: NSLayoutFormatAlignAllCenterX
                                                                                        metrics: @{@"space":spaceBetweenEachButton}
                                                                                          views: NSDictionaryOfVariableBindings(txt1,txt10,txt11,txt12,txt2,txt3,txt4,txt5,txt6, txt7,txt8,txt9)]];

    [self.view addConstraints:constraintsForButtons];
}

そうすると[txt12(==txt1)]、3.5 インチ画面と同じように表示され、下にスペースが残ります。

私はどこで間違いを犯していますか?

4

3 に答える 3

10

PureLayoutをチェックしてください。これは、コードで自動レイアウト制約を作成するための、最も単純で最もプログラマーにとって使いやすい API になるように設計されています。

特定の質問に答えて、PureLayout は、ビューを分散するための 2 つの主要な API を提供します。1 つは各ビュー間の間隔が固定されている (ビュー サイズは必要に応じて変化する) もので、もう 1 つは各ビューのサイズが固定されている (ビュー間の間隔は変化する) ものです。必要に応じて)。後者は、「スペーサービュー」を使用せずに、探しているものを達成します。

// NSArray+PureLayout.h

// ...

/** Distributes the views in this array equally along the selected axis in their superview. Views will be the same size (variable) in the dimension along the axis and will have spacing (fixed) between them. */
- (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis
                                alignedTo:(ALAttribute)alignment
                         withFixedSpacing:(CGFloat)spacing;

/** Distributes the views in this array equally along the selected axis in their superview. Views will be the same size (fixed) in the dimension along the axis and will have spacing (variable) between them. */
- (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis
                                alignedTo:(ALAttribute)alignment
                            withFixedSize:(CGFloat)size;

// ...
于 2013-08-19T05:32:12.107 に答える
2

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutbyExample/AutoLayoutbyExample.htmlその中の間隔とワープを参照してください。ページ、それは素晴らしい説明だと思うので、ここで同じことを説明する必要はありません

編集

上記のリンクはAppleによって無効になりました。iOS 9以降、これらすべてのソリューションであるStackviewが導入されました。

以前は上記のリンクで、答えは@robによって提供された答えと同じでした

于 2014-01-02T04:36:39.690 に答える