UIStackView 隐藏/取消隐藏排列子视图问题

编程入门 行业动态 更新时间:2024-10-07 23:16:43
本文介绍了UIStackView 隐藏/取消隐藏排列子视图问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在隐藏和取消隐藏 dropDownView(由文本字段和按钮组成的 UIView)基于在 Segment Control 中点击的 Segment.以下是视图层次结构:-

I am hiding and unhiding dropDownView(UIView consisting of textfield and button) based on Segment tapped in Segment Control. Following is the view hierarchy :-

以下是隐藏/取消隐藏 dropDownView 的代码:-

Following is the code which hides/unhides dropDownView :-

 private func animateView(view: UIStackView, toHidden hidden: Bool) {
    UIView.animate(withDuration: 0.25) 
    {
    let firstView = view.arrangedSubviews[0]
    firstView.isHidden = hidden
    }
    }
func segmentValueChanged(_ sender: UISegmentedControl) {
        let segmentSelected = segmentControl.selectedSegmentIndex
        switch segmentSelected {
        case 0:
            animateView(view: mainStackView, toHidden: true)
        case 1:
         animateView(view: mainStackView, toHidden: true)
        case 2:
            animateView(view: mainStackView, toHidden: true)
        case 3:
           animateView(view: mainStackView, toHidden: false)
        default:
            break
        }
    }

我面临的问题是在更改段超过 10-15 次后,上述代码停止工作并且下拉视图与段控制重叠,我不知道为什么.感谢您对理解此问题的任何帮助.

Problem which I am facing is after changing segments over 10-15 times the above code stops working and DropDown View overlaps with Segment Control and I am not sure why. Any help to understand this issue is appreciated.

我也试过了
<强>1.setNeedsLayout,
2.设置NeedsDisplay和
3. 将 dropDownView 的高度约束的优先级从 999 降低到1000

推荐答案

似乎在 completionClosure 中的附加设置 isHidden 修复了该问题(Swift 5 语法):

Seem the additional setting isHidden in completionClosure fixes that issue (Swift 5 syntax):

private func animateView(view: UIStackView, toHidden hidden: Bool) {
    let firstView = view.arrangedSubviews[0]
    UIView.animate(withDuration: 0.25) {
        firstView.isHidden = hidden
        view.layoutIfNeeded()
    } completion {
        firstView.isHidden = hidden
    }
}

这篇关于UIStackView 隐藏/取消隐藏排列子视图问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 01:17:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1182565.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列子   视图   UIStackView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!