如何在运行时更改 Safari 应用程序扩展中的工具栏图标?
How Can I change a toolbar icon in a safari app extension at runtime?
我在文档中找到了这个。
https://developer.apple.com/documentation/safariservices/sfsafaritoolbaritem
这让我能够像这样使用 toolbarItemClicked 函数中的 window 变量来获取对工具栏项的引用。
var toolbaritemretrieved = false
window.getToolbarItem { (item) in
toolbaritem = item! as SFSafariToolbarItem;
toolbaritemretrieved = true;
}
一旦我有了对工具栏项目的引用,我就可以 运行 这段代码并据称更改图标的图像。
let path = Bundle.main.path(forResource: "newIcon", ofType: "png")!;
let icon = NSImage(path);
while(!toolbaritemretrieved){
//wait for toolbar item to be retrieved
}
toolbaritem?.setImage(icon);
SFSafariApplication.setToolbarItemsNeedUpdate();
但是这不起作用。没有明显的错误,但由于某种原因图标没有显示新图像。
好的,事实证明此时我的代码实际上没有任何问题,我忘记了一个断点。
我在文档中找到了这个。
https://developer.apple.com/documentation/safariservices/sfsafaritoolbaritem
这让我能够像这样使用 toolbarItemClicked 函数中的 window 变量来获取对工具栏项的引用。
var toolbaritemretrieved = false
window.getToolbarItem { (item) in
toolbaritem = item! as SFSafariToolbarItem;
toolbaritemretrieved = true;
}
一旦我有了对工具栏项目的引用,我就可以 运行 这段代码并据称更改图标的图像。
let path = Bundle.main.path(forResource: "newIcon", ofType: "png")!;
let icon = NSImage(path);
while(!toolbaritemretrieved){
//wait for toolbar item to be retrieved
}
toolbaritem?.setImage(icon);
SFSafariApplication.setToolbarItemsNeedUpdate();
但是这不起作用。没有明显的错误,但由于某种原因图标没有显示新图像。
好的,事实证明此时我的代码实际上没有任何问题,我忘记了一个断点。