# 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](/ios/swiftui/view/layout/grids/vgridforeach/testlittlesquares.md)
* [.draggable()](/ios/swiftui/gestures/drag/.draggable.md)
  {% endtab %}

{% tab title="👥 相關" %}

* convenience extensions for [.frame()](/ios/swiftui/view/layout/frame/.frame.md).
* uses [CGSize](https://developer.apple.com/documentation/coregraphics/cgsize) of [Core Graphics](/ios/swift/scope/framework/built-in-frameworks/core-graphics.md)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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/ios/swiftui/view/layout/frame/ext.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
