iOS视图可见性消失了

时间:2018-02-05 12:46:08

标签: ios swift swift3 view swift4

我是iOS开发的新手。我想从父视图中切换(隐藏/可见)子视图。在android中有一种隐藏可见性的方法。

在android中

subView.setVisibility(View.GONE);

在iOS

subView.removeFromSuperview()

当我使用上面的函数时,它删除了subViewConstraints并弄乱了我的滚动视图约束。

topsubView.bottomAnchor.constraint(equalTo: bottomSubView.topAnchor, constant: 8).isActive = true

当我使用上面的代码时它工作正常并隐藏subView。但是当我想让subView可见时,它没有显示subView。

topsubView.bottomAnchor.constraint(equalTo: bottomSubView.topAnchor, constant: 8).isActive = false
self.view.layoutIfNeeded()

希望你能理解我的问题。谢谢你。

5 个答案:

答案 0 :(得分:21)

因为我曾在iOS和& Android,您需要在ios中使用约束插座来实现Android功能。 iOS不支持像可见性GONE上的Android原生支持一样自动支持VISIBLE

您需要将特定outlet的{​​{1}}(可能是垂直/水平/高度)挂钩,然后将其设置为constraint&需要管理你的用户界面。

要隐藏:

0

显示:

self.viewYourConstraint.constant = 0
self.yourView.hidden = true
self.view.layoutIfNeeded()
  
    

注意:如果由于更新上述约束而影响其他约束,则还必须调用以下内容:

  
self.viewYourConstraint.constant = 100//your constant value
self.yourView.hidden = false
self.view.layoutIfNeeded()

<强>干杯

答案 1 :(得分:9)

试试此扩展程序:

extension UIView {

    func visiblity(gone: Bool, dimension: CGFloat = 0.0, attribute: NSLayoutAttribute = .height) -> Void {
        if let constraint = (self.constraints.filter{$0.firstAttribute == attribute}.first) {
            constraint.constant = gone ? 0.0 : dimension
            self.layoutIfNeeded()
            self.isHidden = gone
        }
    }
}

//如何使用它....

@IBOutlet weak var testView: UIView?
@IBAction func testVisibilty(switchbutton: UISwitch) -> Void {

    let viewHeight:CGFloat = switchbutton.isOn ? 100 : 0.0
    self.testView?.visiblity(gone: !switchbutton.isOn, dimension: viewHeight)

    // set visibility for width constraint
    //let viewWidth:CGFloat = switchbutton.isOn ? 300 : 0.0
    //self.testView?.visiblity(gone: !switchbutton.isOn, dimension: viewWidth, attribute: .width)

}

结果如下:

enter image description here

答案 2 :(得分:3)

您说要隐藏子视图并删除它占用的空间。如果您想避免弄乱约束,可以尝试使用堆栈视图:

  • 创建UIStackView并在堆栈视图及其父视图之间设置适当的约束;
  • 向堆栈视图添加视图;
  • 现在,您可以通过将view.isHidden设置为true或false来切换堆栈视图中这些视图的可见性,并且布局将自动调整。

答案 3 :(得分:3)

也许您更喜欢此解决方案

extension UIView {

    enum Visibility {
        case visible
        case invisible
        case gone
    }

    var visibility: Visibility {
        get {
            let constraint = (self.constraints.filter{$0.firstAttribute == .height && $0.constant == 0}.first)
            if let constraint = constraint, constraint.isActive {
                return .gone
            } else {
                return self.isHidden ? .invisible : .visible
            }
        }
        set {
            if self.visibility != newValue {
                self.setVisibility(newValue)
            }
        }
    }

    private func setVisibility(_ visibility: Visibility) {
        let constraint = (self.constraints.filter{$0.firstAttribute == .height && $0.constant == 0}.first)

        switch visibility {
        case .visible:
            constraint?.isActive = false
            self.isHidden = false
            break
        case .invisible:
            constraint?.isActive = false
            self.isHidden = true
            break
        case .gone:
            if let constraint = constraint {
                constraint.isActive = true
            } else {
                let constraint = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 0)
                self.addConstraint(constraint)
                constraint.isActive = true
            }
        }
    }
}

那么用法是:

someView.visibility = .visible
someView.visibility = .invisible
someView.visibility = .gone

编辑:

提高功能:将在“可见性状态”下的情节提要中起作用(只需写成“可见”,“不可见”,“消失”)

视图内的所有约束应小于1000

extension UIView {

    enum Visibility: String {
        case visible = "visible"
        case invisible = "invisible"
        case gone = "gone"
    }

    var visibility: Visibility {
        get {
            let constraint = (self.constraints.filter{$0.firstAttribute == .height && $0.constant == 0}.first)
            if let constraint = constraint, constraint.isActive {
                return .gone
            } else {
                return self.isHidden ? .invisible : .visible
            }
        }
        set {
            if self.visibility != newValue {
                self.setVisibility(newValue)
            }
        }
    }

    @IBInspectable
    var visibilityState: String {
        get {
            return self.visibility.rawValue
        }
        set {
            let _visibility = Visibility(rawValue: newValue)!
            self.visibility = _visibility
        }
    }

    private func setVisibility(_ visibility: Visibility) {
        let constraints = self.constraints.filter({$0.firstAttribute == .height && $0.constant == 0 && $0.secondItem == nil && ($0.firstItem as? UIView) == self})
        let constraint = (constraints.first)

        switch visibility {
        case .visible:
            constraint?.isActive = false
            self.isHidden = false
            break
        case .invisible:
            constraint?.isActive = false
            self.isHidden = true
            break
        case .gone:
            self.isHidden = true
            if let constraint = constraint {
                constraint.isActive = true
            } else {
                let constraint = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 0)
                // constraint.priority = UILayoutPriority(rawValue: 999)
                self.addConstraint(constraint)
                constraint.isActive = true
            }
            self.setNeedsLayout()
            self.setNeedsUpdateConstraints()
        }
    }
}

答案 4 :(得分:2)

我相信你正在寻找view.isHidden = true。这只是简单地隐藏视图而不改变视图层次结构或约束映射。您随后可以重新显示您的视图。与view.isHidden = false