Frequently asked questions about SwiftUI

Translated from: https://www.hackingwithswift.com/quick-start/swiftui/frequently-asked-questions-about-swiftui

Many people are already asking me questions about SwiftUI, and I have tried my best to ask others who know more to try to find a suitable answer.

So here...

 

Which one to learn: SwiftUI or UIKit?

This question has been asked many times, and I have added a special chapter to this book, so I can speak in more detail: Answer a big question: Should you learn SwiftUI, UIKit, or both at the same time?

 

Where can SwiftUI be used?

SwiftUI runs on iOS 13, macOS 10.15, tvOS 13, and watchOS 6 or any higher version of these platforms. This means that if the application you are using must support iOS N-1 or even N-2 (ie the current version and one or two before that version), then it may even take a year or two for you to consider migrating to SwiftUI .

However, it is important not to treat SwiftUI as a multi-platform framework similar to Java's Swing or React Native. The official statement seems to be that SwiftUI is not a multi-platform framework, but a framework for creating applications on multiple platforms.

It may sound the same, but there is an important difference: Apple is not saying that you can use the same SwiftUI code on every platform, because some things are impossible – you can’t use the Apple Watch’s digital crown on a Mac, for example , And similarly having a tab bar on the watchOS app will not work.

 

Will SwiftUI replace UIKit?

will not. Many parts of SwiftUI are built directly on top of existing UIKit components (such as UITableView). Of course, many other parts are not. They are new controls presented by SwiftUI instead of UIKit.

But the point is not the extent to which UIKit is involved. On the contrary, the point is that we don't care. SwiftUI more or less completely obscures the behavior of UIKit, so if you write an app for SwiftUI and Apple replaces UIKit with a singing elephant in two years, then you don’t have to care – as long as Apple makes the elephant have the same method And the attributes can expose the UIKit to SwiftUI, and your code will not change.

 

Does SwiftUI use auto layout?

Although I will definitely use Auto Layout behind the scenes for some operations, I haven't shown it to us as a SwiftUI designer. Instead, it uses a flexible box layout system, which is familiar to developers from the Web.

 

Is SwiftUI fast?

SwiftUI is amazingly fast-so far, it seems to surpass UIKit in all my tests. After talking with the team that did this, I began to understand why: First, they actively flattened the layer hierarchy, so the system must reduce the drawing work, but in the second step, many operations completely bypassed Core Animation and went directly Metal has carried out extra work speed.

So yes: SwiftUI is very fast, and all of this does not require us to do any additional work.

 

Why can't I see my code preview?

When using SwiftUI, it is very helpful to be able to view the view code and view preview (appearance) at the same time. If you can see the code instead of the preview, you may not have upgraded to macOS 10.15; you must preview it first.

 

How well does the code match the preview?

It will also update the generated code when making any changes to the preview. Similarly, if you change the code, it will also update the user interface. Therefore, the code and the preview are the same and are always in sync.

 

Why does my color look a little off?

SwiftUI provides us with standard system colors, such as red, blue, and green, but these are not pure red, blue, and green that UIColor may be used to. Instead, these are new style colors that can automatically adapt to the light and dark mode, which means their appearance will be brighter or darker based on the appearance of the system.

 

Is UIKit dead?

No! Apple aired a large number of new UIKit features at WWDC this week. If Apple is still talking about the new features of UIKit at WWDC, then you are safe-there is no risk that they will be surprised to eliminate it.

 

Can you mix SwiftUI and UIKit views?

Yes! You can embed one into the other and it works great.

Guess you like

Origin blog.csdn.net/fzhlee/article/details/112541321