Introduction
DyAlertController is a powerful UIAlertController subclass that allows you to present custom alert and action sheets in your iOS apps. It provides additional customization options and features beyond the standard UIAlertController from Apple’s UIKit framework.
Installation
Manual Installation
- Download the DyAlertController library from the official GitHub repository.
- Drag and drop the DyAlertController.framework file into your project.
- Ensure that the framework is added to both the “Embedded Binaries” and “Linked Frameworks and Libraries” sections in your project settings.
CocoaPods Installation
To install DyAlertController using CocoaPods, add the following line to your Podfile:
pod 'DyAlertController'
Then, run the command pod install
in your project directory.
Usage
After installing DyAlertController, you can begin utilizing its features in your iOS app.
Import
Import the DyAlertController module in your swift file:
import DyAlertController
Creating an Alert
To create a new alert, use the following code:
let alertController = DyAlertController(title: "Alert Title", message: "This is a sample alert.", preferredStyle: .alert)
Adding Actions
You can add actions to the alert using the addAction()
method:
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in
// Handle cancel action
}
alertController.addAction(cancelAction)
let okAction = UIAlertAction(title: "OK", style: .default) { (_) in
// Handle OK action
}
alertController.addAction(okAction)
Presenting the Alert
Finally, present the alert by calling present()
or present(animated:completion:)
:
present(alertController, animated: true, completion: nil)
Customization
DyAlertController offers various customization options to tailor your alerts and action sheets according to your app’s design and functionality.
Styling
You can customize the appearance of the alert or action sheet using the appearance
property of DyAlertController:
alertController.appearance.backgroundColor = .white
alertController.appearance.titleColor = .black
alertController.appearance.messageColor = .darkGray
Icon
You can set an icon for your alert using the icon
property:
alertController.icon = UIImage(named: "alert_icon")
Content View
Add a custom view in the content area of the alert using the contentView
property:
alertController.contentView = customView
Animating Presentation
Override the transitionDuration
property to customize the animation duration for presenting and dismissing the alert:
override var transitionDuration: TimeInterval {
return 0.5
}
Conclusion
DyAlertController provides a simple yet powerful way to present custom alerts and action sheets in your iOS app. With its extensive customization options, you can create visually appealing and interactive user interfaces. By following the installation instructions and utilizing the provided examples, you can easily integrate DyAlertController into your project.