Skip to content

Commit 5410ba0

Browse files
committed
Update swiftui.md
1 parent 118f9bb commit 5410ba0

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

libs/notes/swiftui.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ screen.
112112

113113
---
114114

115-
Could you show me how to place a UIViewController (instead of just a UIView)
115+
Could you show me how to place a `UIViewController` (instead of just a `UIView`)
116116
inside SwiftUI views?
117117

118118
---
@@ -216,3 +216,45 @@ This means, for instance, that a SwiftUI `Button` does not become a `UIButton`
216216
on iOS or a `NSButton` on macOS. It remains a SwiftUI `Button`, but it is drawn
217217
and behaves appropriately for the platform it's running on, thanks to the
218218
rendering done by SwiftUI using the native technologies.
219+
220+
---
221+
222+
A vanilla SwiftUI `Button` (rendered on iOS) and a vanilla `UIButton` won't look
223+
the same, will they (without additional styling)?
224+
225+
---
226+
227+
Without additional styling, a SwiftUI `Button` and a UIKit `UIButton` will look
228+
different. By default, SwiftUI `Button` on iOS has a plain text look, without
229+
the rounded-rect border that a UIButton has in its default state.
230+
231+
Here's an example:
232+
233+
SwiftUI `Button`:
234+
235+
```swift
236+
Button("Hello SwiftUI") {
237+
print("Button tapped!")
238+
}
239+
```
240+
241+
UIKit `UIButton`:
242+
243+
```swift
244+
let button = UIButton(type: .system)
245+
button.setTitle("Hello UIKit", for: .normal)
246+
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
247+
```
248+
249+
The SwiftUI `Button` will show just the text "Hello SwiftUI", whereas the UIKit
250+
`UIButton` will show the text "Hello UIKit" inside a rounded-rect border.
251+
252+
This difference is part of SwiftUI's design. In SwiftUI, a `Button` is a role,
253+
not a specific look or behavior. The look and behavior of a SwiftUI `Button` are
254+
determined by the context in which the button is used, and you can easily
255+
customize the button's appearance using modifiers.
256+
257+
That said, it's also worth noting that the look of UI elements can be affected
258+
by many factors, including iOS version, device settings, and app-specific
259+
settings. The details I've provided are typical, but there might be cases where
260+
SwiftUI `Button` and UIKit `UIButton` look similar or even identical.

0 commit comments

Comments
 (0)