You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin iOS:代码添加至UIStackView的UIView无法隐藏求助

解决Xamarin.iOS中StackView新增视图无法隐藏的问题

Hey there! Since you're new to Xamarin.iOS and Xcode, let's break down this issue and fix it step by step—this is a common gotcha with stack views and table view cells, so you're not alone!

核心问题分析

Your main issues are twofold: maintaining a reliable list of your input views, and ensuring hidden views behave correctly within the UIStackView. Let's tackle each part.


1. 正确维护输入视图的引用列表

First off, you need a persistent list in your table view cell class to track all the input views you add—this ensures you can always reference any view later to hide it.

Step 1: Define the list in your cell class

Add a private list property to your cell to store all input views:

// Inside your TableViewCell subclass
private List<UIView> _allInputViews = new List<UIView>();

Step 2: Initialize the list with your default view

In AwakeFromNib, grab the default input view from your stack view and add it to the list:

public override void AwakeFromNib()
{
    base.AwakeFromNib();
    
    // Get the default input view already in the stack view
    var defaultInputView = YourStackView.ArrangedSubviews.FirstOrDefault();
    if (defaultInputView != null)
    {
        _allInputViews.Add(defaultInputView);
    }
}

Step 3: Add new views properly

When creating a new input view, make sure to add it to both the stack view (as an arranged subview) and your list:

private void AddNewInputView()
{
    // Load your input view from its XIB (replace "InputView" with your XIB name)
    var newInputView = NSBundle.MainBundle.LoadNib("InputView", this, null).FirstOrDefault() as UIView;
    if (newInputView == null) return;

    // Match the size of existing views to keep alignment
    newInputView.TranslatesAutoresizingMaskIntoConstraints = false;
    newInputView.WidthAnchor.ConstraintEqualTo(_allInputViews.First().WidthAnchor).Active = true;
    newInputView.HeightAnchor.ConstraintEqualTo(_allInputViews.First().HeightAnchor).Active = true;

    // Add to stack view (critical: use AddArrangedSubview, not AddSubview!)
    YourStackView.AddArrangedSubview(newInputView);
    // Add to your tracking list
    _allInputViews.Add(newInputView);
}

Important: Always use AddArrangedSubview for stack view children—if you use AddSubview, the stack view won't manage the view's layout or hidden state.


2. 实现正确的隐藏逻辑

Now that you have a reliable list, hiding a view is straightforward. Just target the view from the list and update its Hidden property, then trigger a layout refresh:

// Example: Hide the last added input view
private void HideLastInputView()
{
    if (_allInputViews.Count <= 1) return; // Don't hide the default view
    
    var viewToHide = _allInputViews.Last();
    viewToHide.Hidden = true;
    
    // Force the stack view to update its layout
    YourStackView.LayoutIfNeeded();
}

// Or hide a view by index
private void HideInputViewAtIndex(int index)
{
    if (index >= 0 && index < _allInputViews.Count)
    {
        _allInputViews[index].Hidden = true;
        YourStackView.LayoutIfNeeded();
    }
}

3. 处理Table View Cell复用(关键!)

Table view cells get reused, so you need to reset their state when they're recycled. Add this to your cell class to avoid leftover hidden views or duplicate entries:

public override void PrepareForReuse()
{
    base.PrepareForReuse();
    
    // Clear the tracking list
    _allInputViews.Clear();
    
    // Remove all arranged subviews from the stack view
    foreach (var subview in YourStackView.ArrangedSubviews.ToList())
    {
        YourStackView.RemoveArrangedSubview(subview);
        subview.RemoveFromSuperview();
    }
    
    // Re-add the default input view to reset the cell
    var defaultInputView = NSBundle.MainBundle.LoadNib("InputView", this, null).FirstOrDefault() as UIView;
    if (defaultInputView != null)
    {
        YourStackView.AddArrangedSubview(defaultInputView);
        _allInputViews.Add(defaultInputView);
    }
}

常见排查点

If you still have issues, check these:

  • Did you forget to set TranslatesAutoresizingMaskIntoConstraints = false on new views? This breaks auto layout.
  • Are you accidentally hiding the stack view itself instead of the child view?
  • Did you verify that the view reference in your list is the same one in the stack view? Print debug logs to confirm.

内容的提问来源于stack exchange,提问作者Srinidhi Ram

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:10:43