次のコードがあります。
self.noArticlesView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
UIImage *backgroundPattern = [UIImage imageNamed:@"no-articles-background.png"];
self.noArticlesView.backgroundColor = [UIColor colorWithPatternImage:backgroundPattern];
[self.view addSubview:self.noArticlesView];
UIImage *noArticlesImage = [UIImage imageNamed:@"no-articles-icon.png"];
UIImageView *noArticlesImageView = [[UIImageView alloc] initWithImage:noArticlesImage];
noArticlesImageView.translatesAutoresizingMaskIntoConstraints = NO;
[self.noArticlesView addSubview:noArticlesImageView];
NSLayoutConstraint *horizontalCenterConstraint = [NSLayoutConstraint constraintWithItem:noArticlesImageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.noArticlesView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
[self.noArticlesView addConstraint:horizontalCenterConstraint];
NSLayoutConstraint *verticalPlacementConstrant = [NSLayoutConstraint constraintWithItem:noArticlesImageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.noArticlesView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-230.0];
[self.noArticlesView addConstraint:verticalPlacementConstrant];
基本的にnoArticlesView
、View Controllerのビューの上にビュー(と呼ばれる)を追加しています。このビューには、現在追加されているコンテンツがないというメッセージが表示されます(もちろん、ない場合)。
そのビューに、これを示す指標として画像をサブビューとして追加します。
しかし、画像の上記の制約をプロパティを持つように変更し、代わりにtoItem:self.view
追加すると、ビューの下部ではなく上部からプッシュされます(つまり、定数が上部から200pxにプッシュされるため、200)。self.view
self.noArticlesView
noArticlesView
の代わりに画像の制約を相対的に設定することで整理しましたself.view
が、まだその動作に興味があります (自動レイアウトについてはまだ学習中であり、把握したいと考えています)。
また、私が今やっていることは正しいですか?下から 230px に配置したい場合、定数を -230 に設定しますか? 定数を負の悪い形式または何かとして設定していますか?