class ViewController: UIViewController {
@IBAction func clicked(_ sender: Any) {
let vc = UIHostingController(rootView: AlertView(cancel: {
//
self.dismiss(animated: true)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // AppStorage 需要 iOS 14 才可以用 | |
| @propertyWrapper | |
| public struct UserDefault<Value>: DynamicProperty { | |
| @State private var value: Value | |
| let key: String | |
| let store: UserDefaults | |
| public init( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @IBDesignable | |
| class GradientView: UIView { | |
| // implement cgcolorgradient in the next section | |
| @IBInspectable var startColor: UIColor? { | |
| didSet { gradientLayer.colors = cgColorGradient } | |
| } | |
| @IBInspectable var endColor: UIColor? { | |
| didSet { gradientLayer.colors = cgColorGradient } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension String { | |
| // 移除空白 | |
| func removingWhitespace() -> String { | |
| let whitespacePattern = "\\s+" | |
| let regex = try? NSRegularExpression(pattern: whitespacePattern, options: .caseInsensitive) | |
| let range = NSRange(location: 0, length: count) | |
| return regex?.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: "") ?? self | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| import UIKit | |
| /// 创建一个SwiftUI的容器View,将UIViewControler包含在内,即可预览 | |
| /// | |
| /// 如果要预览这个ViewController | |
| /// | |
| /// ```swift | |
| /// class ViewController: UIViewController { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| // MARK: - Initializers | |
| extension UIAlertController { | |
| /// Create new alert view controller. | |
| /// | |
| /// - Parameters: | |
| /// - style: alert controller's style. | |
| /// - title: alert controller's title. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension View { | |
| func hideNavigationBar(_ hidden: Bool = true) -> some View { | |
| if #available(iOS 16.0, *) { | |
| return self.toolbar(hidden ? .hidden : .visible) | |
| } else { | |
| // Fallback on earlier versions | |
| return self.navigationBarHidden(hidden) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension UIGestureRecognizer { | |
| private struct AssociatedKeys { | |
| static var action = "action" | |
| } | |
| private typealias GestureAction = ((UIGestureRecognizer) -> Void) | |
| private var action: GestureAction? { | |
| get { |
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
tableView.moveRow(at: fromIndexPath, to: to)
}
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {