• By w@gner
  • 1 de September de 2024

Creating a Login Screen: UIKit vs. SwiftUI

Creating a Login Screen: UIKit vs. SwiftUI

Creating a Login Screen: UIKit vs. SwiftUI 1024 1024 w@gner

App development is made possible through a series of resources and tools used by developers. One of them is Flutter, an accessible option for various types of companies. Keep reading to learn more.
When developing iOS applications, one of the most common tasks is creating a login screen. This screen typically includes text fields for entering a username and password, labels for guiding the user, a button for submitting the information, a logo at the top, and a background image to enhance the design. Let's explore how to create this screen using two different frameworks: UIKit and SwiftUI.

UIKit Approach

UIKit has been the primary framework for building iOS applications for many years. It provides a more traditional approach where you manage the view hierarchy, constraints, and user interactions using UIViewController and related classes.
Here's a basic implementation of a login screen using UIKit programmatically:

import UIKit
class LoginViewController: UIViewController {
private let logoImageView: UIImageView = {
let imageView = UIImageView(image: UIImage(named: "logo"))
imageView.contentMode = .scaleAspectFit
return imageView
}()
private let usernameTextField: UITextField = {
let textField = UITextField()
textField.placeholder = "Username"
textField.borderStyle = .roundedRect
return textField
}()
private let passwordTextField: UITextField = {
let textField = UITextField()
textField.placeholder = "Password"
textField.borderStyle = .roundedRect
textField.isSecureTextEntry = true
return textField
}()
private let loginButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Login", for: .normal)
button.addTarget(self, action: #selector(loginButtonTapped), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(patternImage: UIImage(named: "background")!)
setupLayout()
}
private func setupLayout() {
view.addSubview(logoImageView)
view.addSubview(usernameTextField)
view.addSubview(passwordTextField)
view.addSubview(loginButton)
logoImageView.translatesAutoresizingMaskIntoConstraints = false
usernameTextField.translatesAutoresizingMaskIntoConstraints = false
passwordTextField.translatesAutoresizingMaskIntoConstraints = false
loginButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
logoImageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 40),
logoImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
logoImageView.widthAnchor.constraint(equalToConstant: 150),
logoImageView.heightAnchor.constraint(equalToConstant: 150),
usernameTextField.topAnchor.constraint(equalTo: logoImageView.bottomAnchor, constant: 40),
usernameTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
usernameTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
passwordTextField.topAnchor.constraint(equalTo: usernameTextField.bottomAnchor, constant: 20),
passwordTextField.leadingAnchor.constraint(equalTo: usernameTextField.leadingAnchor),
passwordTextField.trailingAnchor.constraint(equalTo: usernameTextField.trailingAnchor),
loginButton.topAnchor.constraint(equalTo: passwordTextField.bottomAnchor, constant: 30),
loginButton.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
}
@objc private func loginButtonTapped() {
// Handle login action
}
}

Alternatively, you can use Interface Builder (IB) with .storyboard or .xib files to build this UI. The result will be similar in functionality but with a more visual design approach.

Pros of UIKit

  • Mature & Stable: UIKit has been around for a long time, with extensive documentation and community support.
  • Customizability: Offers a high degree of control over the UI components and layout.
  • Visual Tools: Using .storyboard or .xib, you can visually design your UI, which can be faster and more intuitive for some developers.

Cons of UIKit

  • Verbose Syntax: Even with .storyboard or .xib, you often need to write boilerplate code to manage view controllers, handle state, and update the UI.
  • Imperative UI: Requires you to manually update the UI based on state changes, leading to more boilerplate code.

SwiftUI Approach

SwiftUI represents a modern approach to building UIs with a declarative syntax. You describe the UI and its state, and SwiftUI takes care of the rest.
Here’s how you might create the same login screen using SwiftUI:

import SwiftUI
struct LoginView: View {
@State private var username: String = ""
@State private var password: String = ""
var body: some View {
ZStack {
Image("background")
.resizable()
.edgesIgnoringSafeArea(.all)
VStack(spacing: 20) {
Image("logo")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 150, height: 150)
TextField("Username", text: $username)
.padding()
.background(Color.white)
.cornerRadius(10)
.padding(.horizontal, 20)
SecureField("Password", text: $password)
.padding()
.background(Color.white)
.cornerRadius(10)
.padding(.horizontal, 20)
Button(action: {
// Handle login action
}) {
Text("Login")
.frame(maxWidth: .infinity)
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
.padding(.horizontal, 20)
.padding(.top, 20)
}
}
}
}
struct LoginView_Previews: PreviewProvider {
static var previews: some View {
LoginView()
}
}

Pros of SwiftUI

  • Declarative Syntax: The UI code is more concise and easier to read. You describe what the UI should look like, and SwiftUI handles the rest.
  • Real-Time Previews: SwiftUI provides live previews in Xcode, making it easier to visualize changes.
  • State-Driven: SwiftUI’s state management integrates seamlessly with the UI, reducing the need for boilerplate code.

Cons of SwiftUI

  • Learning Curve: While easier to read, SwiftUI requires learning new concepts like declarative syntax, and it’s different from UIKit.
  • Limited Backward Compatibility: SwiftUI is only available from iOS 13 onwards, limiting its use in apps targeting older versions.

The Advantage of SwiftUI Even with Interface Builder

If you're used to using .storyboard or .xib files in UIKit, you might appreciate the visual design tools they offer. However, SwiftUI provides similar advantages without the need for a separate visual editor:

  • SwiftUI’s Canvas: Offers real-time previews as you code, which can be even more powerful than Interface Builder’s visual tools.
  • Declarative Code: Reduces the need for switching between code and interface files, making the development process smoother.
  • Unified Approach: Everything is in one place, meaning you don’t need to manage separate .storyboard or .xib files. This leads to fewer merge conflicts and simpler version control.

In essence, SwiftUI combines the ease of design you might enjoy with Interface Builder while offering the flexibility and power of a fully code-driven UI.

Conclusion

Both UIKit and SwiftUI have their strengths and weaknesses. UIKit is mature, stable, and offers extensive customization options, particularly if you prefer visual tools like .storyboard or .xib. On the other hand, SwiftUI brings a fresh, modern approach with a more concise and declarative syntax, offering similar visual feedback with its canvas previews.
Choosing Between UIKit and SwiftUI depends on your project requirements:

  • For newer projects or those targeting iOS 13 and above, SwiftUI offers faster development with a modern approach.
  • For projects requiring deep customization, backward compatibility, or integration with existing UIKit code, UIKit with or without Interface Builder may be more practical.

Regardless of which you choose, both are powerful tools that will help you create beautiful and functional UIs for your iOS apps. Happy coding! 🎨📱