How do Swift Playgrounds Work? - swift-playground

I have experience working with Swift 3 to create iOS apps, however I am trying to create a playground (for the WWDC scholarship) and I have no idea how they work.
I downloaded the following sample code from online, and added it to my iOS playground:
import UIKit
import PlaygroundSupport
let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 200))
view.backgroundColor = UIColor.blue
PlaygroundPage.current.liveView = view
However when I press the run button, it says that it is running but nothing happens.
Am I supposed to have an iOS simulator open? Am I supposed to be running a certain app that will show the output?

Did you got your answer if not then use (show assistant editor button )
And then on the left side it would open and you can see the view

Related

SwiftUI NavigationView vs NavigationStack for iOS 15/16

I'm trying to make my iPhone apps (targeting iOS 15 and above) fully compatible with iOS 16 without success!
I don't know how to have NavigationView for iOS 15 and NavigationStack for iOS 16 in the same piece of code.
The code above isn't accepted by Xcode:
if #available(iOS 16, *) {
NavigationStack {
} else {
NavigationView {
}
From: SwiftUI NavigationView/Stack if available iOS 15/16
I guess that creating a custom container, as suggested by #timmy, will be the only solution. But I don't know how to proceed.
What's the solution then?
Personally I wouldn't use NavigationStack unless I would target iOS 16+ but if you want to do that you could make your own Navigation Wrapper
struct MyNavigation<Content>: View where Content: View {
#ViewBuilder var content: () -> Content
var body: some View {
if #available(iOS 16, *) {
NavigationStack(root: content)
} else {
NavigationView(content: content)
}
}
}
and then just use it, like you would NavigationStack or NavigationView and on iOS 15 or older it would use NavigationView and on iOS 16 or newer it would use NavigationStack
Your code isn't accepted by Xcode because it isn't valid. You cannot have a { without a } in the same block.
I have found that using NavigationView can present problems on both iPhone and iPad apps running under iOS 16, even though NavigationView is only deprecated for now. On an iPhone, views reached from a NavigationLink often close themselves as soon as they are opened. On an iPad, the same problem occurs and the generation of Back arrows appears to be a bit random, especially in document apps. I have found it well worth making the effort to use NavigationSplitView and NavigationStack, even though this has involved me writing quite a lot of extra code to achieve pleasing results, particularly in apps designed to run at their best on both iPhone and iPad. That said, Apple do provide some clear advice on how to adopt the new Views here.
I have come across another oddity with iOS 16. Pickers in modal sheets, which have their list arrays populated .onAppear, no longer work as intended and the Picker selection can no longer be set programmatically. You have to populate the Picker's list before activating the modal sheet and pass it to the Sheet as a Binding.
Thanks halo for a top tip on how to use if #available().

In my Mac app, how can I open a local PDF file using a SwiftUI button?

In my Mac app, I want to display a local PDF file through a new view when I tap a button. The StackOverflow post (here) shows how to do it for an iOS app using UIViewRepresentable, but this does not work for a Mac app.
macOS 11, iOS 14
There is a native SwiftUI view modifier for the QuickLook preview since macOS 11 / iOS 14. You can set the binding to the URL to your PDF File (or any other type that can be shown in QuickLook). Then the system presents the file. If the binding value is nil, the preview will not show or will be dismissed if currently shown.
.quickLookPreview(_ item: Binding<URL?>)
This answer is just to add more information to mw_906's answer above. I created a file called UserGuide.pdf and placed it into my app's bundle. The syntax of my ContentView file is
import SwiftUI
import QuickLook
struct ContentView: View {
#State var userGuideUrl: URL?
Button(action: {
userGuideUrl = Bundle.main.url(forResource: "UserGuide", withExtension: "pdf")
} ) {
Text("UserGuide")
}
.help("This button displays the User Guide.")
.quickLookPreview($userGuideUrl)
}
}
Now that Apple has provided such a simple way of displaying files, I encourage all Apple developers to use it to include User Guides and other helpful documentation for their apps.

How do I create a window with a toolbar in macOS Big Sur with SwiftUI?

I have been looking for an answer for an hour. But so far I only find solutions with a NaviagtionView. I do not want to do that. I have a single simple window. Similar to Safari or the system settings.
I am not able to add a toolbar to this window.
What I tried so far:
In AppDelegate is set
window.toolbarStyle = .unified
But it seems to have no effect on the window.
Then I added a toolbar to my ContentView
.toolbar {
ToolbarItem{
Label("Hello", systemImage: "book.circle")
}
Still I see nothing of it.
Update:
It works fine with a new Projekt based on a »SwiftUI App« as Life Cycle. If I choose App Delegate I get no toolbar. Seems to me there is something in the App Delegate I have to set.

UITabBar displays UITabBarItem image ignoring rendering mode AlwaysOriginal

With the release of tvOS 9.1 and Xcode 7.2, my UITabBarItem images are being displayed incorrectly. In my view controllers, I set the tabBarItem.image and tabBarItem.selectedImage with images using UIImageRenderingMode.AlwaysOriginal.
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.tabBarItem.image = UIImage(named: "myTabImage")?.imageWithRenderingMode(.AlwaysOriginal)
self.tabBarItem.selectedImage = UIImage(named: "myTabImageSelected")?.imageWithRenderingMode(.AlwaysOriginal)
}
The selected image displays correctly, but the non-selected image displays as a template, that is, its color information is ignored.
Both images displayed correctly using the tvOS 9.0 SDK, but the non-selected image is displaying incorrectly in tvOS 9.1. To make matters worse, the non-selected images are being shown as black and the tab bar background is also black.
Here is the same code running on tvOS 9.0
I suspect this is a bug with tvOS 9.1, but has anyone found a workaround or see something that I am not doing correctly?
We have seen something similar in our tvos app, except we use text instead of images. tvOS 9.1 ignoring textColor.
UITabBarItem.appearance().setTitleTextAttributes([
NSForegroundColorAttributeName: <barTextColor>
], forState: UIControlState.Normal)
UITabBarItem.appearance().setTitleTextAttributes([
NSForegroundColorAttributeName: <barTextColorSelected>,
], forState: UIControlState.Selected)
It appears to definitely be a bug in the UITabBarController implementation for tvOS 9.1. So I ended up writing my own replacement. While at it, I added support for more than 7 tab bar items, made it look nice on a black background, and included a search bar on one of the tabs (also on a black background). This solves many of the difficulties I faced trying to build my first tvOS app.
Link to Github repository
This is was confirmed as a bug by Apple and has been fixed in tvOS 9.1.1.
It may be help to tvOS 9.1. This code write into the viewDidLoad() of UITabBarController.
for item in self.tabBar.items!{
item.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.blackColor()], forState: UIControlState.Normal)
item.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.greenColor()], forState: UIControlState.Focused)
}

Xcode UI Testing - Drag and Drop

I'm trying to create XCTestCase to test reordering in my outline view (in OS X app). When I use UI Test Recording feature, Xcode prints this:
window.outlines.staticTexts["<cell which I want to move>"].click()
I tried dragging the cell both inside or outside outline view, Xcode generates the same useless code.
Does anyone know how to test drag & drop correctly in Xcode 7?
Not sure if this is new, but in Xcode 7.2, if you look at the header for XCUIElement, there's a clickForDuration(thenDragToElement:) function for OS X. Seems to work for me with NSOutlineView.
This is from the header...
/*!
* Clicks and holds for a specified duration (generally long enough to start a drag operation) then drags
* to the other element.
*/
public func clickForDuration(duration: NSTimeInterval, thenDragToElement otherElement: XCUIElement)
With Xcode 9.4.1
Swift:
func press(forDuration duration: TimeInterval, thenDragTo otherElement: XCUIElement)
Example:
let ele: XCUIElement = XCUIApplication()!.otherElements.matching(identifier: "element_identifier").element(boundBy: 0)
ele.press(forDuration: <timeIntervalInSec>, thenDragTo: <destination_XCUIElement>)
Refer: API Doc
Same issue. No solution. Looks like accessibility does not support d-n-d, and Apple docs say: Provide alternatives for drag-and-drop operations.
The only thing I can suggest is to try checking the accessibility features of the code being tested. UI Testing needs to have accessibility information to be able to understand what is happening in the UI. See the Apple WWDC15 UI Testing video at about 39:30 into the video for an example of how to fix issues where UI recording can't properly generate code.

Resources