---
description: ╱🚧 under construction -> 準備作廢
---

# view + .frame()

{% tabs %}
{% tab title="🌀View" %}
````swift
// 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)
    }
    
}
````
{% endtab %}

{% tab title="📘 手冊" %}
* SwiftUI  ⟩  View  ⟩  [Layout Modifiers](https://developer.apple.com/documentation/swiftui/view-layout) ⟩\
  .[frame(width:height:alignment:)](https://developer.apple.com/documentation/swiftui/view/frame\(width:height:alignment:\))
* [Core Graphics](https://developer.apple.com/documentation/coregraphics) ⟩&#x20;
  * [CGSize](https://developer.apple.com/documentation/coregraphics/cgsize)
  * [CGRect](https://developer.apple.com/documentation/coregraphics/cgrect) ⟩ [insetBy(dx:dy:)](https://developer.apple.com/documentation/coregraphics/cgrect/1454218-insetby)
{% endtab %}

{% tab title="⬇️ 應用" %}
* [testlittlesquares](../grids/vgridforeach/testlittlesquares "mention")
* [.draggable](../../../gestures/drag/.draggable "mention")
{% endtab %}

{% tab title="👥 相關" %}
* convenience extensions for [.frame](.frame "mention").
* uses [CGSize](https://developer.apple.com/documentation/coregraphics/cgsize) of [core-graphics](../../../../swift/scope/framework/built-in-frameworks/core-graphics "mention")to set frame.
{% endtab %}
{% endtabs %}

## History

{% tabs %}
{% tab title="history" %}
1. 2020.10.12：✏️ .frame(\_ size:**CGSize**) 改為 **CGSize?**
2. 2022.02.07: / 簡化 .frame(size) 語法。
{% endtab %}

{% tab title="1" %}
```swift
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)
    }
    
}
```
{% endtab %}

{% tab title="2" %}
```swift
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)
    }
    
}
```
{% endtab %}
{% endtabs %}
