我在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);
}
答案 0 :(得分:3)
以下是我的示例代码:
注意:我将stackview设置为:
分布设置为“填充相等”,Alignement设置为“填充”
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
}
}
答案 1 :(得分:0)
我是Swift的新手,但在stackview中添加带动作的按钮时遇到了类似的问题。
虽然我正在使用#selector(),但我认为我的问题是我没有使用我的按钮变量添加@objc。这样,编译器可能以某种方式解决了目标操作(我不确定)。
请尝试
@objc lazy var button = new UIButton(UIButtonType.Custom);
希望它会有所帮助。
谢谢, 阿迪亚