如何绘制自己的 NSTabView 选项卡?
我想为 NSTabViewItem
绘制自己的选项卡。 我的选项卡应该看起来不同,并且从左上角开始而不是居中。
我怎样才能做到这一点?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想为 NSTabViewItem
绘制自己的选项卡。 我的选项卡应该看起来不同,并且从左上角开始而不是居中。
我怎样才能做到这一点?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
可以将 NSTabView 的样式设置为 Tabless,然后使用 NSSegmentedControl 控制它,该 NSSegmentedControl 是 NSSegmentedCell 的子类以覆盖样式和行为。 要了解如何执行此操作,请查看这个模拟 Xcode 4 样式选项卡的项目:https://github.com /aaroncrespo/WILLTabView/。
it is possible to set the NSTabView's style to Tabless and then control it with a NSSegmentedControl that subclasses NSSegmentedCell to override style and behavior. For an idea how to do this, check out this project that emulates Xcode 4 style tabs: https://github.com/aaroncrespo/WILLTabView/.
绘制选项卡的可能方法之一是使用 NSCollectionView。 下面是 Swift 4 示例:
类
TabViewStackController
包含使用.unspecified
样式和自定义TabBarView
预配置的TabViewController
。TabBarView
类包含表示选项卡的CollectionView
。类
TabViewController
预配置了样式.unspecified
其余类。
其外观如下:
One of possible ways to draw tabs - is to use NSCollectionView. Here is Swift 4 example:
Class
TabViewStackController
containsTabViewController
preconfigured with style.unspecified
and customTabBarView
.Class
TabBarView
containsCollectionView
which represents tabs.Class
TabViewController
preconfigured with style.unspecified
Rest of the classes.
Here is how it looks like:
NSTabView 不是 Cocoa 中最可定制的类,但可以对其进行子类化并进行自己的绘图。 除了维护选项卡视图项的集合之外,您不会使用超类的太多功能,并且最终将实现许多 NSView 和 NSResponder 方法以使绘图和事件处理正常工作。
最好先看看免费或开源选项卡栏控件之一,我使用过 PSMTabBarControl 在过去,它比实现我自己的选项卡视图子类(这是它所取代的)要容易得多。
NSTabView isn't the most customizable class in Cocoa, but it is possible to subclass it and do your own drawing. You won't use much functionality from the superclass besides maintaining a collection of tab view items, and you'll end up implementing a number of NSView and NSResponder methods to get the drawing and event handling working correctly.
It might be best to look at one of the free or open source tab bar controls first, I've used PSMTabBarControl in the past, and it was much easier than implementing my own tab view subclass (which is what it was replacing).
我最近为我正在做的事情做了这个。
我最终使用了 tabless 选项卡视图,然后自己在另一个视图中绘制选项卡。 我希望我的选项卡成为窗口底部状态栏的一部分。
显然,您需要支持鼠标单击,这相当容易,但是您应该确保键盘支持也能正常工作,这有点棘手:您需要运行计时器来在半秒后没有键盘访问后切换选项卡(看看 OS X 是如何做的)。 可访问性是您应该考虑的另一件事,但您可能会发现它确实有效 - 我还没有在我的代码中检查它。
I've recently done this for something I was working on.
I ended using a tabless tab view and then drawing the tabs myself in another view. I wanted my tabs to be part of a status bar at the bottom of the window.
You obviously need to support mouse clicks which is fairly easy, but you should make sure your keyboard support works too, and that's a little more tricky: you'll need to run timers to switch the tab after no keyboard access after half a second (have a look at the way OS X does it). Accessibility is another thing you should think about but you might find it just works—I haven't checked it in my code yet.
我对此非常困惑 - 并发布了 带有背景颜色的 NSTabView - 因为 PSMTabBarControl 现在是过时的还发布了 https://github.com/dirkx/CustomizedTabView /blob/master/CustomizedTabView/CustomizedTabView.m
I very much got stuck on this - and posted NSTabView with background color - as the PSMTabBarControl is now out of date also posted https://github.com/dirkx/CustomizableTabView/blob/master/CustomizableTabView/CustomizableTabView.m
使用单独的 NSSegmentedCell 来控制 NSTabView 中的选项卡选择非常容易。 您所需要的只是一个它们都可以绑定到的实例变量,无论是在文件所有者中,还是在 nib 文件中出现的任何其他控制器类中。 只需在类 Interface 声明中添加类似的内容即可:
然后,在 IB Bindings Inspector 中,将
NSTabView
和NSSegmentedCell
的 Selected Index 绑定到同一个selectedTabIndex
属性。这就是您需要做的全部! 除非您希望默认选定的选项卡索引不为零,否则不需要初始化该属性。 您可以保留选项卡,也可以制作
NSTabView
表,无论哪种方式都可以。 无论哪个控件更改选择,控件都将保持同步。It's very easy to use a separate
NSSegmentedCell
to control tab selection in anNSTabView
. All you need is an instance variable that they can both bind to, either in the File's Owner, or any other controller class that appears in your nib file. Just put something like this in the class Interface declaraton:Then, in the IB Bindings Inspector, bind the Selected Index of both the
NSTabView
and theNSSegmentedCell
to the sameselectedTabIndex
property.That's all you need to do! You don't need to initialize the property unless you want the default selected tab index to be something other than zero. You can either keep the tabs, or make the
NSTabView
tabless, it will work either way. The controls will stay in sync regardless of which control changes the selection.