Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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
Objective c 自动布局:如何隐藏包含子视图的UIView?_Objective C_Uiview_Hide_Autolayout - Fatal编程技术网

Objective c 自动布局:如何隐藏包含子视图的UIView?

Objective c 自动布局:如何隐藏包含子视图的UIView?,objective-c,uiview,hide,autolayout,Objective C,Uiview,Hide,Autolayout,使用新的Autolayout隐藏视图的最佳解决方案无疑是为视图创建高度约束,连接视图并为其创建插座,然后将self.myViewHeightConstriant.constant更改为0。但是假设视图包含一些其他视图,假设一个imageView和它下面的一些标签。现在,imageView距离顶部10px,并且顶部空间到superview的约束值为10px。试图隐藏常量为0的容器UIView在控制台中显示错误: Unable to simultaneously satisfy constraint

使用新的Autolayout隐藏视图的最佳解决方案无疑是为视图创建高度约束,连接视图并为其创建插座,然后将self.myViewHeightConstriant.constant更改为0。但是假设视图包含一些其他视图,假设一个imageView和它下面的一些标签。现在,imageView距离顶部10px,并且顶部空间到superview的约束值为10px。试图隐藏常量为0的容器UIView在控制台中显示错误:

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.   
Try this: (1) look at each constraint and try to figure out which you don't expect; 
(2) find the code that added the unwanted constraint or constraints and fix it.   
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't 
understand, refer to the documentation for the UIView property 
translatesAutoresizingMaskIntoConstraints) 
(
   <NSLayoutConstraint:0xc7cedb0 V:[UIView:0xc7ce1e0(0)]>,
   <NSLayoutConstraint:0xc7ceea0 V:[UIImageView:0xc7ce270]-(0)-|   (Names:  '|':UIView:0xc7ce1e0 )>,
   <NSLayoutConstraint:0xc7cef30 V:|-(10)-[UIImageView:0xc7ce270]   (Names: '|':UIView:0xc7ce1e0 )>
)

Will attempt to recover by breaking constraint 

  <NSLayoutConstraint:0xc7ceea0 V:[UIImageView:0xc7ce270]-(0)-|   (Names: '|':UIView:0xc7ce1e0 )> 
无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试着这样做:(1)看看每个约束,试着找出你不期望的约束;
(2) 找到添加了不需要的约束的代码,然后修复它。
(注意:如果您看到的是NSAutoresizingMaskLayoutConstraints,而您没有看到)
了解,请参阅UIView属性的文档
翻译自动调整大小(从皮肤到肌肉)
(
,
,
)
将尝试通过打破约束进行恢复
问题可能是容器UIView的高度为0,但imageView的顶部空间偏移量为10px,Autolayout引擎不知道如何处理这种情况。尝试为容器视图设置ClipsubView,但没有帮助。有什么想法吗


更新一些想法,为imageView创建outlet topSpaceToSuperView约束并将其Constant也设置为0,看起来不太吸引人。应该有比用多个出口破坏代码更优雅的解决方案…

你不能简单地使用
container.hidden=YES

否则,就是底部约束破坏了一切<代码>@“V:|-10-[imageView]|”告诉容器视图必须至少10磅高。但是
@“V:|-10-[imageView]”
就可以了

与其将imageView锚定到容器底部,不如为imageView的高度设置一个约束

[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[imageView]"
                                        options:nil
                                        metrics:nil
                                          views:views];

[NSLayoutConstraint constraintWithItem:self.imageView
                             attribute:NSLayoutAttributeHeight
                             relatedBy:NSLayoutRelationEqual
                                toItem:self.containerView
                             attribute:NSLayoutAttributeHeight
                            multiplier:1.
                              constant:-10.f];
更新 您在评论中提到imageView的高度不可预测。既然如此,只管理容器的高度可能更容易,但要使用单独的约束:

containerOpen = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[imageView]|"
                                                      options:nil
                                                      metrics:nil
                                                        views:views];

containerClosed = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[containerView(0)]"
                                                        options:nil
                                                       metrics:nil
                                                         views:views];

// Toggle between the constraints to open close the container

- (void)toggleContainer
{
  [self.containerView.superview removeConstraints:containerOpen];
  [self.containerView.superview removeConstraints:containerClosed];

  self.containerView.isOpen = !self.containerView.isOpen;

  if (self.containerView.isOpen)
    [self.containerView.superview addConstraints:containerOpen];
  else
    [self.containerView.superview addConstraints:containerClosed];

  [self.containerView.superview setNeedsUpdateConstraints];
  [self.containerView setNeedsLayout];
  [self.containerView layoutIfNeeded];
}

如果有前导约束、尾随约束、顶部约束或底部约束,则可以在隐藏该约束时将其设置为LessthanRequal,然后在显示该约束时将其设置为equal

因为关系是只读的,所以您可以通过以下方式执行此操作:

  • 超越约束
  • 以编程方式将其从 超级视野
  • 将出口设置为新约束,该约束
    具有相同的参数,除了所需的否之外,我不能仅使用hidden,因为容器视图位于另一个容器视图中,并且还有其他视图。隐藏我的容器视图应该将其他容器视图移到顶部,但简单的隐藏不会做到这一点。另外,imageView是从后端接收的,所以它的高度是动态的,所以我需要它将顶部和底部粘贴到superView,并通过设置containerView高度扩展到所需的大小。但是谢谢,关于底部空间+图像视图高度的好消息。因此,将同时拥有和管理ContainerView和ImageViewHeight。也许,没有其他方法存在。请快速注意约束,我尝试在interface builder中创建约束,而不是在代码中。将业务逻辑(代码)与可视化部分分离更有意义。但是我想这是一个问题…@Centurion我在这里又加了一个例子,因为你说你的图像有不同的高度(即,容器视图的高度取决于图像的高度)。让我知道这是否有用,否则我将删除它。此外,我是一个“拒绝使用界面生成器”的人。我认为它给autolayout增加了很多GUI复杂性,这最终是一件相当简单的事情。但那只是我。