简体   繁体   English

改变视图的约束

[英]changing constraints of a view

I have an Image view that is surrounded by 2 UIVIEWS which are the green top bar and blue side bar.我有一个被 2 个 UIVIEWS 包围的图像视图,它们是绿色的顶栏和蓝色的侧栏。

I have coded a tap gesture [FOR hiding the views], so If you tap once, the top and side bar hide and when tapped again, they re-appear.我编写了一个点击手势[用于隐藏视图],所以如果你点击一次,顶部和侧边栏会隐藏,再次点击时,它们会重新出现。

However, I want to code, that if the bars are visible, then the image view should hug itself to the bars and not extend, so as to prevent clipping.但是,我想编写代码,如果条形可见,则图像视图应将自身拥抱到条形而不延伸,以防止剪切。 Please see attached pic link请看附件图片链接

But when we tap and the bars go away, I want the image view to hug the super view, how can I do that?但是当我们点击和酒吧 go 离开时,我希望图像视图拥抱超级视图,我该怎么做?

I've coded the gesture like so:我已经像这样对手势进行了编码:

@IBAction func test(_ sender: UITapGestureRecognizer) {

print("single tap")

if top.isHidden == false && down.isHidden == false{
    top.isHidden = true
    down.isHidden = true

    print("first")

    
} else{
    top.isHidden = false
    down.isHidden = false
 }

问题图片

You can try playing with anchor constraints.您可以尝试使用锚约束。

For example, put例如,放

imageView.topAnchor.constraint(equalto: topView.bottomAnchor).isActive = true

And same for side View so that they are always constraint to the image view edges.侧视图也是如此,因此它们始终受图像视图边缘的约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改呈现的视图控制器约束 - Changing Presented view controller constraints InterfaceBuilder,更改视图层次结构并保留约束 - InterfaceBuilder, changing view hierarchy and preserve constraints 以编程方式更改视图高度忽略了约束 - programmatically changing height of view ignores constraints 以编程方式更改约束仅影响视图的可见部分 - Changing constraints programmatically only affects visible parts of the view Xcode 8 AutoLayout / Constraints:更改视图以适合每个Iphone屏幕尺寸吗? - Xcode 8 AutoLayout/Constraints: Changing view to fit each Iphone screen size? 为什么在更改视图框架时不再约束“工作”? - Why don't constraints “work” anymore when changing the frame of a view? 调整大小并在景观中放置旋转视图:更新约束还是平移/更改框架? - Resizing and placing a rotated view in landscape: updating constraints or translating/changing frames? 以编程方式更改自动布局约束后,视图未更新 - View is not updating after changing auto-layout constraints programmatically 以编程方式更改约束后,视图不会更新 - View doesn't update after changing constraints programmatically 更改约束时`self.view.layoutIfNeeded()`做什么 - What does `self.view.layoutIfNeeded()` do when changing constraints
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM