iOS/swift

UIColor to Color?, 기존 uicolor색상을 swiftui 에서 사용하기

wiwi_ 2025. 4. 21. 18:06

기존 색상을 그대로 쓰려고하니 에러발생 

.foregroundColor(UIColor.systemBlue)
// Cannot convert value of type 'UIColor' to expected argument type 'Color?'

그냥 Color() 생성자를 쓰면 바로 바꿔진다

.foregroundColor(Color(UIColor.systemBlue))

도대체 왜 그냥 자동고침으로 변경이 안되는걸까?...

 

 

참고로 저 Color의 생성자는 아래와같이 선언되어있다.


extension Color {

    /// Creates a color from a UIKit color.
    ///
    /// Use this method to create a SwiftUI color from a
    /// <doc://com.apple.documentation/documentation/UIKit/UIColor> instance.
    /// The new color preserves the adaptability of the original.
    /// For example, you can create a rectangle using
    /// <doc://com.apple.documentation/documentation/UIKit/UIColor/3173132-link>
    /// to see how the shade adjusts to match the user's system settings:
    ///
    ///     struct Box: View {
    ///         var body: some View {
    ///             Color(UIColor.link)
    ///                 .frame(width: 200, height: 100)
    ///         }
    ///     }
    ///
    /// The `Box` view defined above automatically changes its
    /// appearance when the user turns on Dark Mode. With the light and dark
    /// appearances placed side by side, you can see the subtle difference
    /// in shades:
    ///
    /// ![A side by side comparison of light and dark appearance screenshots of
    ///   rectangles rendered with the link color. The light variant appears on
    ///   the left, and the dark variant on the right.](Color-init-3)
    ///
    /// > Note: Use this initializer only if you need to convert an existing
    /// <doc://com.apple.documentation/documentation/UIKit/UIColor> to a
    /// SwiftUI color. Otherwise, create a SwiftUI ``Color`` using an
    /// initializer like ``init(_:red:green:blue:opacity:)``, or use a system
    /// color like ``ShapeStyle/blue``.
    ///
    /// - Parameter color: A
    ///   <doc://com.apple.documentation/documentation/UIKit/UIColor> instance
    ///   from which to create a color.
    public init(_ color: UIColor)
}

 

반응형