【问题标题】:how to set visibility GONE like android in IOS?如何在IOS中像android一样设置可见性GONE?
【发布时间】:2016-09-17 07:29:12
【问题描述】:

有人知道隐藏标签并让屏幕的其他视图使用留空位置的简单方法吗?并在显示该视图时进行相反的操作。像 Android setVisibility = GONE for layers 之类的东西。

据我所知,使用 setHidden=true 只会从屏幕上隐藏视图,但不会重新排列它周围的任何东西。

谢谢

【问题讨论】:

  • 你不能在 iOS 中像 android 那样做到这一点,但如果你使用 UIStackView 你可以
  • 使用 UIStackView 有什么作用? @Fonix
  • 如果您对此有任何参考代码,请发表评论
  • @siddharth shah :您也可以使用自动布局约束来实现它
  • 我尝试使用自动布局约束,但标签在其他视图中不可见...@SandeepBhandari

标签: android ios iphone uilabel show-hide


【解决方案1】:

在 iOS 上实现 Android .GON​​E 功能的唯一方法是使用 UIStackView

通过Apples documentation

动态更改堆栈视图的内容堆栈视图 每当添加、删除或添加视图时自动更新其布局 插入到排列的Subviews 数组中,或者每当其中一个 排列子视图的隐藏属性更改。

SWIFT 3:

// Appears to remove the first arranged view from the stack.
// The view is still inside the stack, it's just no longer visible, and no longer contributes to the layout.
let firstView = stackView.arrangedSubviews[0]
firstView.hidden = true

SWIFT 4:

let firstView = stackView.arrangedSubviews[0]
firstView.isHidden = true

【讨论】:

  • 完美答案。
【解决方案2】:

您可以使用 AutoLayout 约束轻松实现此目的。

假设你有这样三个视图:

+-----+
|  A  |
+-----+
+-----+
|  B  |
+-----+
+-----+
|  C  |
+-----+

你想让视图 B 在某些情况下消失。

如下设置约束(这些只是示例值):

B top space to A:  4
C top space to B:  4
B height: 20

然后在代码中为 B 的高度创建一个 NSLayoutConstraint 出口。通过在 IB 中拖放约束来做到这一点。

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bHeight;

最后,要让视图消失,只需执行以下操作:

self.bHeight = 0;

请注意,如果您对表格视图单元格执行此操作,您可能会遇到希望 B 出现在某些单元格中而不出现在其他单元格中的情况。

在这种情况下,您必须将高度重置为您希望其可见的那些单元格的“正常”值。

self.bHeight = 24;

【讨论】:

  • 如果你只设置self.bHeight = 0;,那么A和C之间会有一个双空格,你需要将顶部空格之一设为0,否则如果有超过3个视图,它看起来不均匀
  • 如果您使用 ARC 并遇到错误:>“ARC 不允许将 'int' 隐式转换为 'NSLayoutConstraint *'”,您必须使用 self.bHeight.constant = 24;
【解决方案3】:

我一直在寻找简单的解决方案并找到了它。我不必使用 UIStackView 或创建约束出口。就用这个吧:

class GoneConstraint {
    private var constraint: NSLayoutConstraint
    private let prevConstant: CGFloat

    init(constraint: NSLayoutConstraint) {
        self.constraint = constraint
        self.prevConstant = constraint.constant
    }

    func revert() {
        self.constraint.constant = self.prevConstant
    }
}


fileprivate struct AssociatedKeys {
    static var widthGoneConstraint: UInt8 = 0
    static var heightGoneConstraint: UInt8 = 0
}


@IBDesignable
extension UIView {

    @IBInspectable
    var gone: Bool {
        get {
            return !self.isHidden
        }
        set {
            update(gone: newValue)
        }
    }

    weak var widthConstraint: GoneConstraint? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.heightGoneConstraint) as? GoneConstraint
        }
        set(newValue) {
            objc_setAssociatedObject(self, &AssociatedKeys.widthGoneConstraint, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    weak var heightConstraint: GoneConstraint? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.heightGoneConstraint) as? GoneConstraint
        }
        set(newValue) {
            objc_setAssociatedObject(self, &AssociatedKeys.heightGoneConstraint, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }

    private func update(gone: Bool) {
        isHidden = gone
        if gone {
            for constr in self.constraints {
                if constr.firstAttribute == NSLayoutAttribute.width {
                    self.widthConstraint = GoneConstraint(constraint: constr)
                }
                if constr.firstAttribute == NSLayoutAttribute.height {
                    self.heightConstraint = GoneConstraint(constraint: constr)
                }
                constr.constant = 0
            }
        } else {
            widthConstraint?.revert()
            heightConstraint?.revert()
        }
    }
}

现在,您可以拨打view.gone = true 就可以了。

【讨论】:

  • @siddharth shah 我没有测试这个。
  • 好的,没问题我会尝试然后回答你顺便说一句谢谢@DanielQ
【解决方案4】:

如果你的应用支持 ios 9 及更高版本,你可以使用 UIStackView。

但如果您的应用也支持 ios 8,那么您必须使用自动布局来实现它并为视图添加高度约束

所以如果你想隐藏而不是仅仅设置高度约束值 0。

【讨论】:

    猜你喜欢
    • 2016-06-24
    • 1970-01-01
    • 2013-04-16
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    相关资源
    最近更新 更多