🌀view + .frame()
╱🚧 under construction -> 準備作廢
// 2022.02.14 + (method) .frame(cgfloat)
import SwiftUI
// 🌀View+frame()
extension View {
    
    /// Examples:
    /// ```
    /// view.frame(size)
    /// view.frame(nil)
    /// ```
    public func frame(_ size: CGSize?, alignment: Alignment = .center) -> some View {
        frame(width: size?.width, height: size?.height, alignment: alignment)
    }
    
    /// `view.frame(w, h)`
    public func frame(_ width: CGFloat, _ height: CGFloat) -> some View {
        frame(width: width, height: height)
    }
    
    /// `view.frame(cgfloat)`
    public func frame(_ size: CGFloat) -> some View {
        frame(width: size, height: size)
    }
    
}- SwiftUI ⟩ View ⟩ Layout Modifiers ⟩ .frame(width:height:alignment:) 
- convenience extensions for .frame(). 
- uses CGSize of Core Graphicsto set frame. 
History
- 2020.10.12:✏️ .frame(_ size:CGSize) 改為 CGSize? 
- 2022.02.07: / 簡化 .frame(size) 語法。 
import SwiftUI
// 🌀View + .frame()
extension View {
    
    /// Example: `view.frame(size)`
    public func frame(_ size: CGSize) -> some View {
        frame(width: size.width, height: size.height)
    }
    
    /// Example: `view.frame(100, 200)`
    public func frame(_ width: CGFloat, _ height: CGFloat) -> some View {
        frame(width: width, height: height)
    }
    
}import SwiftUI
// 🌀view.frame()
extension View {
    // 🌀view.frame(size)
    @ViewBuilder public func frame(_ size: CGSize?) -> some View {
        if size != nil {
            frame(width: size!.width, height: size!.height)
        } else { self }
    }
    
    // 🌀view.frame(w, h)
    public func frame(_ width: CGFloat, _ height: CGFloat) -> some View {
        frame(width: width, height: height)
    }
    
}Last updated
Was this helpful?