SwiftUI For Dummies. Wei-Meng Lee
Чтение книги онлайн.

Читать онлайн книгу SwiftUI For Dummies - Wei-Meng Lee страница 10

Название: SwiftUI For Dummies

Автор: Wei-Meng Lee

Издательство: John Wiley & Sons Limited

Жанр: Программы

Серия:

isbn: 9781119652700

isbn:

СКАЧАТЬ programming language so that you’re prepared to dive into the world of SwiftUI.

      Introducing SwiftUI

      IN THIS CHAPTER

      

Understanding what SwiftUI is

      

Getting the tools for SwiftUI

      

Comparing UIKit to SwiftUI

      

Using the preview canvas and Live Preview

      

Understanding the various files in a SwiftUI project

      I know the feeling of being on the verge of learning something new. If you’re anything like me, you’re eager to try things out and see how it feels. And that’s exactly what you do in this chapter!

      In this chapter, I explain what SwiftUI is, show you how SwiftUI has changed the user interface (UI) development paradigm, and explain how SwiftUI makes the process easier going forward. Then I tell you how you can get started with the necessary tools. Finally, with the tools that you’ve installed, you create your first iOS application using SwiftUI, and learn how the various components in your project work together as a whole.

      SwiftUI is a declarative programming framework for developing UIs for iOS, iPadOS, watchOS, tvOS, and macOS applications. In fact, SwiftUI was invented by the watchOS group at Apple.

      The following shows a simple implementation in UIKit and Storyboard. Here, a Button and Label view have been added to the View Controller in Storyboard; two outlets and an action have been created to connect to them:

       class ViewController: UIViewController {

      

       @IBOutlet weak var lbl: UILabel!

       @IBOutlet weak var button: UIButton!

       @IBAction func btnClicked(_ sender: Any) {

       lbl.text = "Button tapped"

       }

      For laying out the views, you use auto-layout to position the button and label in the middle of the screen (both horizontally and vertically).

      To customize the look and feel of the button, you can code it in the loadView() method, like this:

       override func loadView() {

       super.loadView()

      

       // background color

       button.backgroundColor = UIColor.yellow

      

       // button text and color

       button.setTitle("Submit", for: .normal)

       button.setTitleColor(.black, for: .normal)

      

       // padding

       button.contentEdgeInsets = UIEdgeInsets(

       top: 10, left: 10, bottom: 10, right: 10)

      

       // border

       button.layer.borderColor =

       UIColor.darkGray.cgColor

       button.layer.borderWidth = 3.0

      

       // text font

       button.titleLabel!.font =

       UIFont.systemFont(ofSize: 26, weight:

       UIFont.Weight.regular)

      

       // rounder corners

       button.layer.cornerRadius = 10

      

       // auto adjust button size

       button.sizeToFit()

       }

      FIGURE 1-1: UIKit is event driven, and it uses delegates to handle events.

       struct ContentView: View {

       СКАЧАТЬ