gpt4 book ai didi

android - iOS 相当于 View.GONE

转载 作者:可可西里 更新时间:2023-11-01 03:06:54 26 4
gpt4 key购买 nike

是否有与 Android View.GONE 等效的 iOS 版本?

在 Android 中,将 View 设置为 GONE 将使其不可见,并确保 View 不占用布局中的任何空间。我知道在 iOS 中,您可以使用

将 View 设置为隐藏
[viewName setHidden:true];

但 View 仍然占用布局空间,我认为完全删除 View 并稍后重新创建它的效率很低。

(注意:我看过这个帖子:iOS equivalent for Android View.GONE visibility mode 但没有接受的答案,将高度设置为 0 对我不起作用,因为在我的 View 被删除后页面上的后续 View 没有移动)

最佳答案

constant = 0 的宽度/高度约束添加到 your View 将使 your View 的 width 和 height = 0(喜欢它消失)

// set the height constraint to 0 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:theGoneView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:0]];

// set the width constraint to 0
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:theGoneView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:0]];

在 swift

// set the width constraint to 0
let widthConstraint = NSLayoutConstraint(item: theGoneView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 0)
view.addConstraint(widthConstraint)

// set the height constraint to 0
let heightConstraint = NSLayoutConstraint(item: theGoneView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 0)
view.addConstraint(heightConstraint)

或者这个 UIView 扩展

extension UIView {        

func goAway() {
// set the width constraint to 0
let widthConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 0)
superview!.addConstraint(widthConstraint)

// set the height constraint to 0
let heightConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 0)
superview!.addConstraint(heightConstraint)
}

}

希望对你有帮助

关于android - iOS 相当于 View.GONE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25627042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com