I am trying to implement tab control in Xamarin.Mac, programmatically(i.e. without the interface builder). Would like to show 2 NSViews in 2 tabs inside the window. Looked at different documentations but could not find a proper solution on this. Can someone help with a sample implementation of NSTabView/NSTabViewcontroller using Xamarin.Mac ? Or point to a sample implementation if available?
Sorry i misunderstood, here is sample for NSTabView .
this.View.TranslatesAutoresizingMaskIntoConstraints = false; NSTabView tabView = new NSTabView(); this.View.AddSubview(tabView); //add tab NSTabViewItem item1 = new NSTabViewItem(); item1.Identifier = new NSString("Tab1"); item1.Label = "Tab1"; tabView.Add(item1); NSTabViewItem item2 = new NSTabViewItem(); item2.Identifier = new NSString("Tab2"); item2.Label = "Tab2"; tabView.Add(item2); tabView.DidSelect += (sender, e)=> { if(e.Item.Identifier.ToString() == "Tab1") { } else if (e.Item.Identifier.ToString() == "Tab2") { } }; tabView.TranslatesAutoresizingMaskIntoConstraints = false; this.View.AddConstraint(NSLayoutConstraint.Create(tabView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this.View,NSLayoutAttribute.Left, 1, 0)); this.View.AddConstraint(NSLayoutConstraint.Create(tabView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this.View, NSLayoutAttribute.Top, 1, 0)); this.View.AddConstraint(NSLayoutConstraint.Create(tabView, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this.View, NSLayoutAttribute.Right, 1, 0)); this.View.AddConstraint(NSLayoutConstraint.Create(tabView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.View, NSLayoutAttribute.Bottom, 1, 0));
which is recommended to use - NSTabView or NSTabViewController ?
It depends on the scenarios you met , NSTabViewController is more Convenient, it has a in-built TabView , you do not have to initialize it at first ,but i prefer to use NSTabView in NSViewContoller , it is more flexible .
Answers
Refer to https://gist.github.com/wozuo/53a475e67dd11c60cfb1e4f62ea91d32, i convert it from swift to c# .
@ColeX : Thanks for the prompt response. However, I was looking for "tab" control(using NSTabView or NSTabViewController). The code you shared is for table view. Could you share a sample for NSTabView/NSTabViewController ? Also, which is recommended to use - NSTabView or NSTabViewController ?
Sorry i misunderstood, here is sample for NSTabView .
It depends on the scenarios you met , NSTabViewController is more Convenient, it has a in-built TabView , you do not have to initialize it at first ,but i prefer to use NSTabView in NSViewContoller , it is more flexible .
@ColeX : Thank you ! This helps :-)