将子视图添加到UistackView中,无用按钮



我在ViewDidLoad中的堆栈视图中添加了一堆视图。最后,我添加了一个"加载更多"按钮,该按钮将重复ViewDidLoad中工作的代码 - 添加更多布置的子视图。但是我看不到屏幕上的任何视图。非常感谢任何帮助。

public override void ViewDidLoad()
{
  stackView.AddArrangedSubview(view1);
  stackView.AddArrangedSubview.(view2);
  stackView.AddArrangedSubview.(button);
  button.TouchUpInside += (object sender, EventArgs e) =>
  {
    stackView.AddArrangedSubview(view1);
    stackView.AddArrangedSubview.(view2);
}

这是我的示例代码基础:

注意:我将stackView设置为:

分布设置为"填充平等",将对齐设置设置为"填充"

public override void ViewDidLoad()
{
    base.ViewDidLoad();
    // Perform any additional setup after loading the view, typically from a nib.
    AddViewsInStackView(); // Add views first time in the stackview
    AddButtonInStackView(); // Add the "Add button" first time in the stackview
}
private void AddViewsInStackView()
{
    // Create 2 views and add them to the stackview
    var view1 = new UIView { BackgroundColor = UIColor.Red };
    var view2 = new UIView { BackgroundColor = UIColor.Green };
    stackView.AddArrangedSubview(view1);
    stackView.AddArrangedSubview(view2);
}
private void AddButtonInStackView()
{
    // create the "Add button" 
    var button = new UIButton(UIButtonType.Custom);
    button.SetTitle("Add", UIControlState.Normal);
    button.SetTitleColor(UIColor.Blue, UIControlState.Normal);
    button.TouchUpInside += (sender, e) => AddSupplementaryViews();
    stackView.AddArrangedSubview(button); // Add the "Add button" in the stackview
}
private void AddSupplementaryViews()
{
    if (stackView.ArrangedSubviews.Any())
    {
        // Fetch the "Add button" then remove them
        var addButton = stackView.ArrangedSubviews.Last();
        stackView.RemoveArrangedSubview(addButton); // This remove the view from the stackview
        addButton.RemoveFromSuperview(); // This remove the view from the hierarchy
        AddViewsInStackView(); // Add new views
        AddButtonInStackView(); // Add it again so it will be the last one every time
    }
}

我是Swift的新手

尽管我正在使用#Selector(),但我认为我的问题是我没有使用按钮变量添加@OBJC。这样,可能以某种方式无法由编译器解决目标动作(我不确定)。

请尝试

@OBJC懒惰var button = new uibutton(uibuttontype.custom);

希望这会有所帮助。

谢谢aditya

相关内容

  • 没有找到相关文章

最新更新