Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift – 像macOS上的Photos.app一样的macOS应用程序大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在尝试创建像Photos.app这样的macOS应用. NSWindowController有一个带分段控件的工具栏.当您点击分段控件时,它会更改NSWindowController中的NSViewController. 到目前为止我所拥有的是带有NSViewController的NSWindowController.我已经将NSWindowController子类化了,其中我有一个方法,只要
我正在尝试创建像Photos.app这样的macOS应用. NSWindowController有一个带分段控件的工具栏.当您点击分段控件时,它会更改NSWindowController中的NSViewController.

swift – 像macOS上的Photos.app一样的macOS应用程序

到目前为止我所拥有的是带有NSViewController的NSWindowController.我已经将NSWindowController子类化了,其中我有一个方法,只要用户点击分段控件就会调用它.

基本上,无论单击哪个段,它都将实例化所需的视图控制器并将其设置为NSWindowController的contentViewController属性.

swift – 像macOS上的Photos.app一样的macOS应用程序

这是正确的做法吗?

此外,我想,NSWindowController应该具有可以切换到的每个NSViewControllers的属性,这些属性会被延迟加载(当用户点击它们时加载它们并且它们被重新用于防止重新初始化).

码:

import Cocoa

class MainWindowController: NSWindowController
{
    var secondaryViewController:NSViewController?

    override func windowDidLoad()
    {
        super.windowDidLoad()

        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
    }

    @IBACtion func segmentedControlDidChange(_ sender: NSSegmentedControl)
    {
        print("Index: \(sender.SELEctedSegment)")

        if sender.SELEctedSegment == 3 {
            if secondaryViewController == nil {
                let viewController = storyboard?.instantiateController(withIdentifier: "SecondaryViewController") as! NSViewController
                secondaryViewController = viewController
            }

            self.window?.contentViewController = self.secondaryViewController
        }
    }
}

我是macOS开发的新手,但是,我已经做了很长一段时间的iOs.如果有更好的方法,我想了解它.谢谢!!!

解决方法

要将标签/分段控件移动到标题栏,您需要:

>将工具栏添加到窗口,并将控件添加到工具栏,
>隐藏标题

class TopLevelWindowController: NSWindowController {

  override func windowDidLoad() {
    super.windowDidLoad()

    if let window = window {

        // reminder like style
        // window.titlebarAppearsTransparent = true
        window.titleVisibility = .hidden
        // window.styleMask.insert(.fullSizeContentView)
    }
  }
}

>现在,工具栏将合并到顶部栏位置.

大佬总结

以上是大佬教程为你收集整理的swift – 像macOS上的Photos.app一样的macOS应用程序全部内容,希望文章能够帮你解决swift – 像macOS上的Photos.app一样的macOS应用程序所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。