For components conforming to the protocol named DefaultCocoaViewBridging, you can get UIKit/Cocoa objects as follows.
DefaultCocoaViewBridging gets the UIView object from SwiftUI.View.
In contrast, DefaultCocoaControllerBridging gets the UIViewController object.
The CocoaBriding protocol defines a DefaultCocoaType.
For example, for Toggle, the DefaultCocoaType is UISwitch(iOS).
It can be handled as follows
However, if the ToggleStyle is set to Button, UIButton is used internally instead of UISwitch.
For such cases, it is also possible to retrieve the data by specifying the type as follows.
The method of specifying the type is defined in SwiftUI.View’s and does not need to conform to the DefaultCocoaViewBridging or DefaultCocoaControllerBridging protocols.
If the specified type is not found, the closure will not be called.
Support additional component
extension XXView: DefaultCocoaViewBridging { // confirms `DefaultCocoaViewBridging`
public typealias DefaultCocoaViewType = XXCocoaView // UIKit/Cocoa type
}
extension YYView: DefaultCocoaViewControllerBridging { // confirms `DefaultCocoaViewControllerBridging`
public typealias DefaultCocoaControllerType = YYCocoaViewController // UIKit/Cocoa type
}
LifeCycle Event Modifiers
In some View lifecycle events, a modifier is provided to retrieve the obtained UIKit/Cocoa object.
As an example, the following code hides the tabBar on push and redisplays it on pop.
TabView {
NavigationView {
List(0..<100) { i in
NavigationLink {
Text("Detail: \(i)")
.cocoa(for: CocoaViewController.self) { vc in
print(vc)
}
.onViewWillAppear { vc in
// Hide TabBar
vc?.tabBarController?.tabBar.isHidden = true
}
.onViewWillDisappear { vc in
// Show TabBar
vc?.tabBarController?.tabBar.isHidden = false
}
} label: {
Text("Row: \(i)")
}
}
}
}
The following modifiers are available.
onViewWillAppear
onViewDidLoad
onViewWillDisappear
onViewDidDisAppear
SwiftUI and Cocoa correspondence table
This may vary depending on the operating system and usage conditions.
CocoaUI
Obtain and customize UIKit/Cocoa objects from SwiftUI components.
Demo
For example, Slider uses UISlider internally. Therefore, it can be customized by directly referencing the UISlider object as follows.
Document
For components conforming to the protocol named
DefaultCocoaViewBridging
, you can get UIKit/Cocoa objects as follows.DefaultCocoaViewBridging
gets the UIView object from SwiftUI.View. In contrast,DefaultCocoaControllerBridging
gets the UIViewController object. The CocoaBriding protocol defines aDefaultCocoaType
. For example, for Toggle, the DefaultCocoaType is UISwitch(iOS). It can be handled as followsSpecify type
However, if the ToggleStyle is set to
Button
,UIButton
is used internally instead ofUISwitch
. For such cases, it is also possible to retrieve the data by specifying the type as follows.The method of specifying the type is defined in SwiftUI.View’s and does not need to conform to the
DefaultCocoaViewBridging
orDefaultCocoaControllerBridging
protocols. If the specified type is not found, the closure will not be called.Support additional component
LifeCycle Event Modifiers
In some View lifecycle events, a modifier is provided to retrieve the obtained UIKit/Cocoa object. As an example, the following code hides the tabBar on push and redisplays it on pop.
The following modifiers are available.
SwiftUI and Cocoa correspondence table
This may vary depending on the operating system and usage conditions.