首页 > 解决方案 > 如何使用可可和c#在xcode中的故事板中制作菜单

问题描述

我想做很多像杀毒软件一样的菜单。我想要它,所以当您单击左侧的众多按钮之一时,所有其他按钮将禁用,而该按钮将保持启用状态。它还将隐藏和取消隐藏故事板中的某些元素。我使用的是视觉工作室,但故事板使用的是 xcode。它会是什么样子的图像

这就是它的样子。然后在左边它将显示我想要的元素。

标签: c#xcodemacosvisual-studioxamarin

解决方案


我们可以在代码中实现这一点,只需放置一系列按钮并在单击特定按钮时移除其他按钮。

public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            List<NSButton> array = new List<NSButton>();
            for (int i = 0; i < 10; i++)
            {
                NSButton button = new NSButton();
                button.Frame = new CGRect(5, 40 * i + 10, 100, 30);
                button.Title = i.ToString();
                button.Font = NSFont.SystemFontOfSize(20);
                this.View.AddSubview(button);
                array.Add(button);

                button.Activated += (sender, e) =>
                {
                    foreach(var b in array)
                    {
                        if(b != button)
                        {
                            b.RemoveFromSuperview();
                        }
                    }
                };               
            }
        }

推荐阅读