滚动在子视图中不起作用 - swift(iOS)

发布于 2025-01-11 07:47:57 字数 2479 浏览 1 评论 0原文

输入图片这里的描述

我在containerview中添加了子视图,containerview高度会随着子视图高度的变化而更新。当Bzcardview显示时,它将隐藏containerview,当containerview显示时,它将隐藏Bzcardview。 Bazcardview和containerview都有相同的约束。

func UpdateSubviewHeight()  {
    
    if  segmentHeaderView.selectedSegmentIndex == 0
    {
       
        self.containerviewHeighContraint.constant =  InformationTabView.InnerviewHeightContraint.constant + 50
    }
    else {
        self.containerviewHeighContraint.constant = self.BzcardView.view.height
    }
    self.view.layoutIfNeeded()
}

private func addChildViewController(vc: UIViewController) {
  
    self.Containerview.addSubview(vc.view)
    if vc == FirstTabView
    {
        self.containerviewHeighContraint.constant =  InformationTabView.InnerviewHeightContraint.constant
    }
    else {
       self.containerviewHeighContraint.constant = vc.view.height
    }
    self.view.layoutIfNeeded()
       

    activateRequiredConstraints(for: vc.view)
    vc.didMove(toParent: self)
}

private func activateRequiredConstraints(for childView: UIView) {
    childView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        childView.leadingAnchor.constraint(equalTo: Containerview.leadingAnchor, constant: 0),
        childView.trailingAnchor.constraint(equalTo: Containerview.trailingAnchor, constant: 0),
        childView.topAnchor.constraint(equalTo: Containerview.topAnchor, constant: 0),
        childView.bottomAnchor.constraint(equalTo: Containerview.bottomAnchor, constant: 0),
        childView.heightAnchor.constraint(equalTo: Containerview.heightAnchor, constant: 0)
    ])
} 

当选项卡更改时,它只执行隐藏和显示操作。

if segmentedControl.selectedSegmentIndex == 0
    {
        self.InformationTabView.view.isHidden = false
        self.ExposuresView.view.isHidden = true
        self.Bzcardview_.isHidden = true
        self.scrollview.isScrollEnabled = true
        self.Bzcardview_.isHidden = true
        self.Bzcardview_.isUserInteractionEnabled = false
    }
    else if segmentedControl.selectedSegmentIndex == 1
    {
        self.InformationTabView.view.isHidden = true
        self.Bzcardview_.isHidden = false
        self.Bzcardview_.isUserInteractionEnabled = true
        self.ExposuresView.view.isHidden = true
        self.scrollview.isScrollEnabled = false
    }

enter image description here

I have added child view inside the containerview, containerview height will update as subview height changed.when Bzcardview display,it will hide containerview and when containerview display it will hide Bzcardview. Bazcardview and containerview both having same contraint.

func UpdateSubviewHeight()  {
    
    if  segmentHeaderView.selectedSegmentIndex == 0
    {
       
        self.containerviewHeighContraint.constant =  InformationTabView.InnerviewHeightContraint.constant + 50
    }
    else {
        self.containerviewHeighContraint.constant = self.BzcardView.view.height
    }
    self.view.layoutIfNeeded()
}

private func addChildViewController(vc: UIViewController) {
  
    self.Containerview.addSubview(vc.view)
    if vc == FirstTabView
    {
        self.containerviewHeighContraint.constant =  InformationTabView.InnerviewHeightContraint.constant
    }
    else {
       self.containerviewHeighContraint.constant = vc.view.height
    }
    self.view.layoutIfNeeded()
       

    activateRequiredConstraints(for: vc.view)
    vc.didMove(toParent: self)
}

private func activateRequiredConstraints(for childView: UIView) {
    childView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        childView.leadingAnchor.constraint(equalTo: Containerview.leadingAnchor, constant: 0),
        childView.trailingAnchor.constraint(equalTo: Containerview.trailingAnchor, constant: 0),
        childView.topAnchor.constraint(equalTo: Containerview.topAnchor, constant: 0),
        childView.bottomAnchor.constraint(equalTo: Containerview.bottomAnchor, constant: 0),
        childView.heightAnchor.constraint(equalTo: Containerview.heightAnchor, constant: 0)
    ])
} 

When tab changed it just perform hide and show action.

if segmentedControl.selectedSegmentIndex == 0
    {
        self.InformationTabView.view.isHidden = false
        self.ExposuresView.view.isHidden = true
        self.Bzcardview_.isHidden = true
        self.scrollview.isScrollEnabled = true
        self.Bzcardview_.isHidden = true
        self.Bzcardview_.isUserInteractionEnabled = false
    }
    else if segmentedControl.selectedSegmentIndex == 1
    {
        self.InformationTabView.view.isHidden = true
        self.Bzcardview_.isHidden = false
        self.Bzcardview_.isUserInteractionEnabled = true
        self.ExposuresView.view.isHidden = true
        self.scrollview.isScrollEnabled = false
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱没勇气 2025-01-18 07:47:57
override func layoutSubviews() {
    super.layoutSubviews()
    DispatchQueue.main.async {
        
        if self.Bzcardview_.isHidden{
            self.containerviewHeighContraint.constant = vc.view.height
            self.containerviewHeighContraint.constant = self.BzcardView.view.height
        }else{
            self.containerviewHeighContraint.constant =  InformationTabView.InnerviewHeightContraint.constant + 50
            self.containerviewHeighContraint.constant =  InformationTabView.InnerviewHeightContraint.constant
            
        }
    }
}
override func layoutSubviews() {
    super.layoutSubviews()
    DispatchQueue.main.async {
        
        if self.Bzcardview_.isHidden{
            self.containerviewHeighContraint.constant = vc.view.height
            self.containerviewHeighContraint.constant = self.BzcardView.view.height
        }else{
            self.containerviewHeighContraint.constant =  InformationTabView.InnerviewHeightContraint.constant + 50
            self.containerviewHeighContraint.constant =  InformationTabView.InnerviewHeightContraint.constant
            
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文