【问题标题】:How to Hide an Element and Its Space in Swift如何在 Swift 中隐藏元素及其空间
【发布时间】:2015-08-18 17:28:52
【问题描述】:

我想隐藏一个元素(在我的例子中是数据选择器)并隐藏它的空间。所以我用动画尝试了这个:

@IBAction func showQnt(sender: AnyObject) {
    if (self.pickerQnt.alpha == 0.0) {
        UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.ShowHideTransitionViews, animations: {
            self.pickerQnt.alpha = 1.0
            
            
            }, completion: {
                (finished: Bool) -> Void in
                //var constH = NSLayoutConstraint(item: self.pickerQnt, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 162)
                //self.pickerQnt.addConstraint(constH)
        })
    } else {
        UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.ShowHideTransitionViews, animations: {
            self.pickerQnt.alpha = 0.0
            
            
            }, completion: {
            (finished: Bool) -> Void in
                // CHECK: ?!? constrain to set view height to 0 run time
                //var constH = NSLayoutConstraint(item: self.pickerQnt, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 0)
                //self.pickerQnt.addConstraint(constH)
        })
    }
}

我也尝试过类似的东西

self.pickerQnt.hidden = true

但它不会工作。

提前致谢。

【问题讨论】:

    标签: ios swift hide


    【解决方案1】:

    在 Objective-C/Swift 中的视图上使用 "hidden" property 实际上不会“折叠”视图占用的空间(与 "View.GONE" in Android 相反)。

    您应该使用自动布局和高度约束。

    在您的 Xib/Storyboard 文件中创建一个高度约束。给它想要的高度。为该约束创建一个 IBOutlet(从 Constraints 列表中的 Constraint 中按住 ctrl-drag 到您的源文件),然后您可以编写此方法

    Swift 解决方案

    var pickerHeightVisible: CGFloat!
    ...
    ...
    func togglePickerViewVisibility(animated: Bool = true) {
        if pickerHeightConstraint.constant != 0 {
            pickerHeightVisible = pickerHeightConstraint.constant
            pickerHeightConstraint.constant = 0
        } else {
            pickerHeightConstraint.constant = pickerHeightVisible
        }
        if animated {
             UIView.animateWithDuration(0.2, animations: {
                  () -> Void in
                   self.view.layoutIfNeeded()
             }, completion: nil)
        } else {
             view.layoutIfNeeded()
        }
    }
    

    Objective-C 解决方案:

    @property (nonatomic, strong) CGFloat pickerHeightVisible;
    ...
    ...
    - (void)togglePickerViewVisibility:(BOOL)animated {
        if(pickerHeightConstraint.constant != 0) {
            pickerHeightVisible = pickerHeightConstraint.constant;
            pickerHeightConstraint.constant = 0;
        } else {
            pickerHeightConstraint.constant = pickerHeightVisible;
        }
        if(animated) {
             [UIView animateWithDuration:0.2
                 animations:(void (^)(void))animations:^(void) {
                      [self.view layoutIfNeeded];
                 }];
        } else {
              [self.view layoutIfNeeded];
        }
    }
    

    免责声明:我没有测试或编译上面的代码,但它会让你知道如何实现它。

    重要提示:上面的代码假定您在故事板/笔尖中为选取器视图的高度约束设置了一个大于 0 的值。

    【讨论】:

    • 5 岁的答案,我知道,但我从您的评论中学到了很多东西。我喜欢 Swift,但我绝对讨厌 UI 方面。从来没有理解过如何在 iOS 中使用“View.Gone”。非常感谢您的回答
    • @Zun 非常感谢!我一直讨厌 UIKit……你试过 SwiftUI 了吗?更好的 UI 框架!但在其他方面很难,导航有点棘手。
    【解决方案2】:

    这是一个老问题,但有另一个选项可用于较新的 iOS 版本:

    如果您的布局允许,并且如果您的目标是iOS9 或更高版本,您可以将您的视图安排在UIStackView 作为容器。堆栈视图的子视图在隐藏时会折叠,即不再占用任何空间。

    动态更改堆栈视图的内容

    每当视图被添加、删除或插入到排列的子视图数组中,或者排列的子视图的 isHidden 属性之一发生变化时,堆栈视图都会自动更新其布局。

    (https://developer.apple.com/documentation/uikit/uistackview#overview)

    【讨论】:

      【解决方案3】:

      最好的方法是在堆栈中添加视图,并在隐藏一个视图时自动调整堆栈视图的高度。但是,如果视图不在堆栈视图中,则只需执行以下操作。

      1- 隐藏视图。
      2- 为视图分配高度约束,使视图的高度约束出口

      @IBOutlet weak var myView: UIView!
      @IBOutlet weak var myViewHeightConstraint: NSLayoutConstraint!
      myView.isHidden = true
      viewHeightConstraint.constant = 0
      

      【讨论】:

        【解决方案4】:

        一个简单的方法是在隐藏视图时将高度设置为零(这也是没有高度约束的IBOutlet)。

        myView.isHidden = true
        myView.heightAnchor.constraint(equalToConstant: CGFloat(0)).isActive = true
        

        并在取消隐藏时再次设置高度

        myView.isHidden = false
        myView.heightAnchor.constraint(equalToConstant: CGFloat(40)).isActive = true
        

        要通过动画实现上述功能,只需将 heightAnchor 代码放在UIView.animate() 块中

        如果你必须多次这样做,那么你可以通过在扩展中制作一个函数来将你的两行代码减少到一行。

        【讨论】:

          猜你喜欢
          • 2016-01-18
          • 1970-01-01
          • 2016-11-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-25
          相关资源
          最近更新 更多