22:35. Delight
I looked WWDC 2019 Key Notes. The expected declarative UI really became a reality, and this is truly a universal event for the world of iOS development. “We need to write an article about this,” I thought, and thousands of iOS developers around the world who are in a state of exaltation, thought.
05:30 Tutorials
Swift UI - a new framework developed by Apple, written in Swift, is intended for the declarative description of the UI in the code.
I noticed that every year in terms of documentation at Yabloko it becomes steeper and steeper. This time, under Swift UI, they filled in several full-fledged tutorials with step-by-step addition and interactive display of the result on the view, and at the end added carefully control questions to consolidate what was passed. Well, just a fairy tale! In the same place - links to example-projects.
Handsomely!
I will not retell the tutorial in Russian, in such a beautiful form it is better to poke it in the original source. I will describe my impressions and observations about this whole story with Swift UI and I will indulge in it a little.
07:00 Installation
The new Xcode has a new code editing mode - Editor And Canvas.
I didn’t see the canvas right away - for that, it’s not enough to download Xcode 11.0, you need to update Makos to 10.15. Without it, Xcode will work, but without the charms in the form of canvas and, perhaps, something else.
I was pleased that when you select the code, the corresponding element in the canvas will also be highlighted.
New axis, exampl'y run. Is it painted? Well, yes, it happens. Is the backlight falling off? No, of course - the same never happened in Xcode;) But the canvas works, and changes to the view are reflected instantly if it is not a table with complex cells.
09:22. New project
When creating a new project, the Use Swift UI option is now available and the project is created with the appropriate configuration.
Immediately struck by the new file SceneDelegate
, which creates a window and its root view. But in AppDelegate
there is not a word about it. But there is a new method that creates UISceneConfiguration
:
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { // Called when a new scene session is being created. // Use this method to select a configuration to create the new scene with. return UISceneConfiguration(name: «Default Configuration», sessionRole: connectingSceneSession.role) }
Well, the Default Configuration
itself is in Info.plist
and SceneDelegate
indicated there. Everything fell into place.
But back to SceneDelegate
- the launch takes place in it.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). // Use a UIHostingController as window root view controller let window = UIWindow(frame: UIScreen.main.bounds) window.rootViewController = UIHostingController(rootView: ContentView()) self.window = window window.makeKeyAndVisible() }
UIHostingController
is a generic UIViewController
that can have any type of content under the new View
protocol
open class UIHostingController<Content> : UIViewController where Content : View { /// }
View
protocol is simple to the obscene:
public protocol View : _View { associatedtype Body : View var body: Self.Body { get } }
That is, he needs only to implement the body
.
But the trick is that a whole ton of extensions is written on this View
protocol, for example, for hanging gestures, for tracking the appearance and disappearance of a view from the screen, for indents, frames, and many, many things. You can see all this in the dock View | Apple Developer Documentation . This means that any view you create (under the View
protocol) out of the box will get a bunch of super powers!
Let's ContentView.swift
on to ContentView.swift
.
struct ContentView : View { var body: some View { Text(«Hello World») } }
It's simple: we create a view from the already implemented Views and Controls | Apple Developer Documentation . Could make it more complicated using different containers View Layout and Presentation | Apple Developer Documentation and the views that we have already created.
Layout with Swift UI is a separate story, about which many more materials will be written, and Apple has a decent tutorial. I will not dwell on it. Let's return to the second part of ContentView.swift
, there is such code there:
#if DEBUG struct ContentView_Previews : PreviewProvider { static var previews: some View { ContentView() } } #endif
Obviously by name, it is he who is responsible for what is displayed in the canvas - and it will display the previews
, in our case ContentView()
.
Let's try to create a screen with a primitive table:
struct ContentView : View { var birds: [Birds] = [] var body: some View { List(birds) { bird in Text(verbatim: bird.name) } } }
Everything. Is done. Simple, concise, elegant. Here it is all the beauty of the declarative UI!
It can be seen that the List
is a table under the hood. But not UITableView, but some UpdateCoalesingTableView.
And it is also clear that there is no autoloout. There are no contstaint's, everything is on frames, which means that there are no these complex systems with linear equations and a bunch of calculations. But, on the other hand, the layout is adaptive and the frame is somehow calculated and we will see in future sessions of WWDC exactly how.
Swift UI - is it wrapper over UIKit? It seems that yes, and at the same time no.
Joe Groff tweets the following:
"Some entities are wrappers over UI / NS views, but the fact that they do it, and what type of view they wrap is a thing that can change."
Original:
There are some things that you can use. A distinct thing.
What noticed more:
BindableObject
, ObjectBinding
and the whole Combine framework.11:00 Total
Alas, all these charms
@available (iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
That is, to switch to them is to abandon the old axes, which is too radical and devoid of concern for the user.
I am sure that the mechanism is still raw, there will be many more changes, and a lot of bugs will emerge, and this is natural. But after a year or two it will be possible to introduce in the prod.
Swift UI is a direct revolution in the world of people who call themselves iOS developers, and it's cool that this new road, even if not ideal, has opened.
In the meantime, nothing prevents you from using it in your pet projects, filling your hand and getting esthetic pleasure :)
Source: https://habr.com/ru/post/454750/
All Articles