0

目標は、ポートレート モードでもランドスケープ モードでも、イメージがスーパー ビューの左端から 20 の位置に配置され、viewHolder もスーパー ビューの下端から 20 の位置に配置されることです。

私がしていることは

ViewController.h

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *verticalConsImage;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UIView *viewHolder;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *bottomConsViewHolder;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topConsViewHolder;

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view removeConstraint:self.verticalConsImage];
    [self.view removeConstraint:self.topConsViewHolder];
    [self.view removeConstraint:self.bottomConsViewHolder];

    //the position of the imageView left edge is equal of the superview’s left edge plus 20.
    self.verticalConsImage = [NSLayoutConstraint
                                      constraintWithItem:self.imageView
                                      attribute:NSLayoutAttributeLeading
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.view
                                      attribute:NSLayoutAttributeLeading
                                      multiplier:1.0f
                                      constant:20.0f];
    // add it again to the view
    [self.view addConstraint:self.verticalConsImage];
    //the position of the viewHolder's bottom edge is equal of the superview’s bottom edge plus 20.    
    self.bottomConsViewHolder = [NSLayoutConstraint
                                      constraintWithItem:self.viewHolder
                                      attribute:NSLayoutAttributeBottom
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.view
                                      attribute:NSLayoutAttributeBottom
                                      multiplier:1.0f
                                      constant:20.0f];
    // add it again to the view
    [self.view addConstraint:self.bottomConsViewHolder];

ただし、アプリを実行すると、viewHolder は縦向きにも横向きにもまったく表示されません。以下に画像を添付します ここに画像の説明を入力

ここに欠けているもの、これについて何かアイデアがあれば助けてください。ありがとう

4

1 に答える 1

1

IB のビューの上部に黄色で強調表示されているビューから制約を削除し、すべてのコードを削除する必要があります。その必要はありません。システムがその制約を削除できない場合は、そのビューに固有のサイズがないため、最初に固定の高さを指定してから、上部の制約を削除する必要があります。

ビューが消えた理由は、これらの 2 つの制約が IB で 4 インチの画面に設定され、シミュレーターが 3.5 インチの画面を使用しているためだと思います。これらの制約を維持するために変更できるのは、ビューの高さだけです。 、おそらくゼロ以下に設定されました。

于 2013-02-06T21:03:22.843 に答える