I would like to use SF Symbols in my macOS project. How to implement one?
Button(action: {}) {
Image(systemName: "star") //Error: 'init(systemName:)' is unavailable in macOS
}
It is nativly supporting from the macOS 11 beta or later, then it works as usual, otherwise you have to export a template and import it to the assets catalog, then you can use it as a normal image. So:
if #available(OSX 11.0, *) {
Image(systemName: "trash.fill")
} else {
Image("trash.fill") // Imported as a supporting format like PDF (not SVG)
}
Another way is to use the symbol directly in the text:
Text("") // The symbol itself can not be shown on the markdown of the StackOverflow
Remember that you should embed the font in your application or the destination should have the SF Symbols App installed
Apple's Human Interface Guidelines state:
You can use SF Symbols in apps running in iOS 13 and later, watchOS 6 and later, and tvOS 13 and later.
No Mac support at this time. ☹️
Before using this code you should install SF Symbols app
I make Image for macOS like below
that's how it looks on macOS
Related
I have some strange error in my SPM.
Code example:
import SwiftUI
struct ExampleView: View {
var body: some View {
Text("Stub")
}
}
Error:
'some' return types are only available in macOS 10.15.0 or newer
But I have macOS Ventura 13.0.1, & XCode 14.1. What could be the problem?
Thanks.
The code itself looks fine. As #Timmy mentioned, there's probably something wrong with the project settings. Try upping the deployment target...
Go to your project settings by selecting your project in Project Navigator.
Select your project
And change the deployment target
Problem solved.
I needed to change simulator from Mac (by Default) to iPhone.
I have a simple app with an iOS target that is configured for iPhone, iPad, and Mac, making it a Catalyst app. On Xcode 12.0 Beta 1, I tried adding a Settings scene to my app. The documentation page says it is compatible with both macOS 11.0+ and
Mac Catalyst 14.0+. The entirety of my app is this:
import SwiftUI
#main
struct TestSettingsApp: App {
#SceneBuilder var body: some Scene {
WindowGroup {
Text("Hello, world!").padding()
}
#if os(macOS)
Settings {
Text("Settings here.")
}
#endif
}
}
Based on the WWDC 2020 What's new in SwiftUI video (4:50), I would expect this to automatically add a "Preferences..." menu option, which would show "Settings here." However, this option never shows up. I also tried replacing #if os(macOS) with #if targetEnvironment(macCatalyst), which had no effect.
It turns out that Apple's documentation was either incorrect early on in the beta period, or they decided not to implement Settings for Mac Catalyst. The documentation page I referred to earlier has since been updated to remove "Mac Catalyst 14.0+".
I'm using Xcode 11.1 and all of my swift previews show the following errors:
Cannot preview in this file -- current target needs adjusted build settings
along with
"ProjectTests.xctest' failed to get build
settings:
unsupportedProductType("com.apple.product-type.bundle.unit-test", <Xc..."
Clicking the diagnostics button generates the following popup:
Open file has supported build settings
"ProjectTests.xctest" failed to get build settings: unsupportedProductType("com.apple.product-type.bundle.unit-test", <Xcode3TargetProduct:0xblahblahblahProjectTests.xctest blueprint:< Xcode3Target:0xblahblahProjectTests>>)
My previews still render fine, but is there some way to remove this error?
Change Destination to some Simulator instead of "iOS Generic Device" or in preview provider specify preview device explicitly, like
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewDevice("iPhone 11")
}
}
Xcode Version 11.1 (11A1027)
This error no longer exists in Xcode Version 11.2 (11B52).
I build an app with the 2nd beta of Xcode 11, using SwiftUI, and everything worked completely fine, but now, with the 3rd beta, the app isn't even build. I get errors like "Use of undeclared type 'View'", "Unknown attribute 'State'" etc. What can I do? Is the problem in my code or is it just a bug?
The problem is when I'm trying to build the app to run on my Mac (with UIKit for Mac)
#State var score = 0
The error is "Unknown attribute 'State'"
struct ContentView : View {
/*...*/
}
The error is "Use of undeclared type 'View'"
Got same issue on Xcode 11.2. Found out that the reason was that somewhere in same module I declared protocol State {}. This breaks compiler without any reasonable errors.
This is from the Xcode 11 Beta 3 Release Notes.
Known Issues
Xcode 11 doesn’t support working with SwiftUI in an iPad app brought to macOS. (41416222, 51201699)
I downloaded the Xcode 11 Beta 3 myself just to confirm the errors you mentioned, and I had the same issues when trying to build a SwiftUI project for Mac.
I would suggest going back to using Xcode Beta 2 if possible. Unfortunately, Apple doesn't provide download links to previous betas, so hopefully you have a Time Machine backup or Beta 2 still installed on your computer. Otherwise, you might be out of luck until the next beta is released.
I was getting the same "Unknown attribute 'State'" issue in Beta 5. But I was doing "#state" and not "#State". Case matters. It's hard to notice because both ways are shown as purple.
If you are doing a Kotlin multi-platform project, check if your shared module has a component named "State".
Instead #main, please replace #UIApplicationMain.
I've attached screenshots.
https://prnt.sc/vimvwn
I got the same error while trying to compile a Swift project created with Xcode 13 on Xcode 11. The problem was that the newer version uses the SDK 12.1 while the older one uses the SDK 10.15. To let the project compile on the older Xcode/SDK I just performed these steps:
remove the #main attribute from the swift source code
add Cocoa.framework in the section Frameworks, Libraries, and Embedded Content
add a file named main.m to the project (no bridge header) with this content:
#include <Cocoa/Cocoa.h>
int main(int argc, const char * argv[]) {
return NSApplicationMain(argc, argv);
}
I'm trying to run the Automatic Preview in Xcode 11, however even though the project builds successfully, the automatic preview fails giving me the error Failed to build ContentView.swift. Failed to code sign ContentView.swift.
I have just created the project without any modifications to the auto generated code. Even the code signing has also been set properly. Note I'm using the Free Apple Developer Account. Could this cause any problem?
Following is the code from ContentView.swift file:
import SwiftUI
struct ContentView : View {
var body: some View {
Text("Hello World")
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
First select the Xcode using following command
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
Then run
sudo xcodebuild -license
Hope it is helpful
I solved that problem by going to the Xcode - Preferences -> Locations and assigning the Command Line Tools to Xcode 11. Also keep in mind that it is a beta, so it is unstable.
I had this problem as I was using // to comment beside my curly brackets to check I had them all in the right spot in my screen code. Xcode seems to cope with a few but once the screen gets too large and too many comments......
Now I comment on the line below and no problems!
Xcode SwiftUI Previews may occasionally fail with an error message about a modified .h file, such as “file Header.h has been modified since the module file Module.pcm was built: mtime changed.” (85938686)
Workaround: Delete the Clang module cache by running the following command in Terminal:
rm -rf "$TMPDIR/../C/clang/ModuleCache"
Then try to preview the file again.
https://developer.apple.com/documentation/Xcode-Release-Notes/xcode-13_3-release-notes
I had this problem too. I solved it by moving the file location of the file I was trying to preview. When the file was created it was down below the resources folder, so I moved it up and then preview worked.
I am currently having this problem with XCode 12.2, it says "Failed to build ContentView.swift." and when you click on the information's button it says "Extra tokens at end of #SourceLocation".
Apparetly that was because i wrote a "// Comment" in the same line, next to a variable definition, so i solved this only moving the comment to the next line.
I don't know who i can help this, but just wanted to share it.
Regards!
Change your iOS version to the latest one. It will solve your issue.
Renaming main project solved same issue on my Mac.
Rename the blue file in your project navigation.
I encountered this problem too. I download Xcode beta-1 before I download macOS 10.15. Then after my system upgrade to 10.15, the preview function didn't work. It turns out that Apple has released Xcode beta-2 version recently, so I downloaded and it worked. So, KEEP THE NEWEST VERSION OF XCODE!!!!