Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么我对superview';它是从superview';什么是上衣?现在是对的吗?_Ios_Objective C_Uiview_Autolayout_Nslayoutconstraint - Fatal编程技术网

Ios 为什么我对superview';它是从superview';什么是上衣?现在是对的吗?

Ios 为什么我对superview';它是从superview';什么是上衣?现在是对的吗?,ios,objective-c,uiview,autolayout,nslayoutconstraint,Ios,Objective C,Uiview,Autolayout,Nslayoutconstraint,我有以下代码: 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 colorWith

我有以下代码:

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
),该视图显示一条消息,表示当前没有添加任何内容(当然,在没有添加的情况下)

然后在该视图中添加一个图像作为子视图,作为该视图的指示器

但是,当我将图像上的上述约束更改为将属性
添加到tem:self.view
并将其添加到
self.view
而不是
self.noArticlesView
时,它会从视图的顶部而不是底部推送它(即,200作为常量,它会从顶部推送200px)

我通过设置图像相对于
noArticlesView
的约束,而不是
self.view
,来解决这个问题,但我仍然对其行为感到好奇(我仍在学习自动布局,并希望掌握它)



还有,我现在做的对吗?如果我想把它定位在离底部230px的位置,将常数设置为-230是正确的方法吗?是否将常量设置为负的坏形式或任何东西?

我也经历过类似的奇怪事情,我怀疑这是因为superview(在您的例子中是
self.view
)没有高度--它不会根据其superview调整大小。这意味着
self.view
的底部与顶部相等,使其看起来像是约束从顶部起作用。你可以尝试两件事来验证这个理论:

  • 检查
    self.view
    的框架(高度为0)
  • 设置
    self.view.clipsToBounds=YES
    (您的视图,包括背景视图,由于被剪裁而不会显示)
  • 这只是(或可能是)对所发生的事情的一种解释,我不确定正确的处理方法是什么


    至于你的另一个问题:这样做是完全正确的,尽管通常有更好的方法。问问自己“为什么是-230”?如果这是因为图像高度为230,则使用
    -noArticlesImage.size.height
    。如果230恰好是使整个视图看起来最好的数字,则可以使用它(尽管最佳实践要求使用常量或预处理器宏来定义230)。

    无法要求更好的答案。230确实是一个神奇的数字,但我会记住这一点,谢谢。:)