Introduction
Magnetic is an open-source layout framework designed for both iOS and macOS platforms. Developed by Francesco D'Alessio, it was first introduced on October 22, 2021. This framework allows you to place views on the screen and automatically snap them to the screen edges.
Key Features
The primary features of this project include:
- Cross-Platform Compatibility: Magnetic supports both iOS and macOS, making it versatile for various app development needs.
- Diverse Layout Possibilities: It offers a range of magnetic layouts to choose from, allowing flexible customization.
- User-Friendly: Magnetic is designed with ease of use in mind, making it accessible for developers of all levels.
Usage Instructions
To incorporate Magnetic into your Xcode project, follow these steps:
- Import the Magnetic folder into your Xcode project.
- Create a MagneticView object and specify the desired layout type.
- Add views to the MagneticView object.
For detailed API documentation, refer to the project's GitHub page.
As of October 16, 2023, the project's GitHub repository boasts 1.5k stars and 300 forks.
Highlights
Here are the notable highlights of Magnetic:
- User-Friendly: Magnetic's user-friendly design simplifies layout management.
- Versatile Layouts: It offers a variety of magnetic layouts to suit different design requirements.
- Cross-Platform Compatibility: Magnetic seamlessly works on both iOS and macOS.
Magnetic is an ideal choice for iOS and macOS developers seeking to create magnetic layouts on their screens.
Usage Example
Below is a code example demonstrating the usage of Magnetic in Swift:
import UIKit
import Magnetic
class ViewController: UIViewController {
private let magneticView = MagneticView(type: .topLeft)
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(magneticView)
// Add a view
let view = UIView()
view.backgroundColor = .red
magneticView.addSubview(view)
}
}
This code snippet creates a MagneticView object with the specified layout type (in this case, topLeft). It then adds a UIView to the MagneticView object.
Magnetic supports various layout types, such as top, bottom, left, right, center, centerHorizontally, and centerVertically.
Magnetic Layout Types
- topLeft: Views snap to the top-left corner of the screen.
- top: Views snap to the top of the screen.
- bottom: Views snap to the bottom of the screen.
- left: Views snap to the left side of the screen.
- right: Views snap to the right side of the screen.
- center: Views are centered on the screen.
- centerHorizontally: Views are horizontally centered on the screen.
- centerVertically: Views are vertically centered on the screen.