Last active
October 11, 2024 14:54
-
-
Save Koshimizu-Takehito/fc2b217d0b7ea76abd92f99b6da5f27f to your computer and use it in GitHub Desktop.
UIViewControllerRepresentableのレイアウト処理のサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import SwiftUI | |
class SampleViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let traits = [UITraitPreferredContentSizeCategory.self] | |
registerForTraitChanges(traits) { (self: Self, _) in | |
self.view.invalidateIntrinsicContentSize() | |
} | |
} | |
} | |
struct ViewControllerRepresentable<ViewController: UIViewController>: UIViewControllerRepresentable { | |
func makeUIViewController(context: Context) -> ViewController { | |
ViewController() | |
} | |
func updateUIViewController(_ uiViewController: ViewController, context: Context) {} | |
func sizeThatFits( | |
_ proposal: ProposedViewSize, | |
uiViewController: ViewController, | |
context: Context | |
) -> CGSize? { | |
let size = CGSize( | |
width: proposal.width ?? UIView.noIntrinsicMetric, | |
height: proposal.height ?? UIView.noIntrinsicMetric | |
) | |
return uiViewController.view.systemLayoutSizeFitting( | |
size, | |
withHorizontalFittingPriority: .fittingSizeLevel, | |
verticalFittingPriority: .defaultLow | |
) | |
} | |
} | |
#Preview { | |
@Previewable @State var count = 0 | |
var dynamicTypeSize: DynamicTypeSize { | |
let allCases = DynamicTypeSize.allCases | |
return allCases[count % allCases.count] | |
} | |
ViewControllerRepresentable<SampleViewController>() | |
.fixedSize() | |
.padding() | |
.background(Color.blue.secondary) | |
.clipShape(.rect(cornerRadius: 10)) | |
.contentShape(.rect(cornerRadius: 10)) | |
.frame(maxWidth: .infinity, maxHeight: .infinity) | |
.background(.background) | |
.environment(\.dynamicTypeSize, dynamicTypeSize) | |
.tint(.pink) | |
.onTapGesture { count += 1 } | |
Text(String(describing: dynamicTypeSize)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="23502" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES"> | |
<device id="retina6_12" orientation="portrait" appearance="light"/> | |
<dependencies> | |
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23504"/> | |
<capability name="Image references" minToolsVersion="12.0"/> | |
<capability name="Safe area layout guides" minToolsVersion="9.0"/> | |
<capability name="System colors in document resources" minToolsVersion="11.0"/> | |
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> | |
</dependencies> | |
<objects> | |
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="SampleViewController" customModule="RepresentableSample" customModuleProvider="target"> | |
<connections> | |
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/> | |
</connections> | |
</placeholder> | |
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/> | |
<view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="i5M-Pr-FkT"> | |
<rect key="frame" x="0.0" y="0.0" width="393" height="852"/> | |
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> | |
<subviews> | |
<stackView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="1000" horizontalCompressionResistancePriority="1000" alignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="Jah-Wg-omT"> | |
<rect key="frame" x="0.0" y="59" width="393" height="793"/> | |
<subviews> | |
<stackView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="1000" horizontalCompressionResistancePriority="1000" axis="vertical" alignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="9Ig-3r-V8f"> | |
<rect key="frame" x="0.0" y="342.66666666666669" width="393" height="107.66666666666669"/> | |
<subviews> | |
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="751" verticalCompressionResistancePriority="751" adjustsImageSizeForAccessibilityContentSizeCategory="YES" translatesAutoresizingMaskIntoConstraints="NO" id="kDE-ij-fsY"> | |
<rect key="frame" x="166.66666666666666" y="-0.66666666666666785" width="60" height="34.666666666666671"/> | |
<imageReference key="image" image="rainbow" catalog="system" symbolScale="large" renderingMode="original"/> | |
<preferredSymbolConfiguration key="preferredSymbolConfiguration" configurationType="font" scale="large" weight="black"> | |
<fontDescription key="fontDescription" style="UICTFontTextStyleTitle1"/> | |
</preferredSymbolConfiguration> | |
</imageView> | |
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="1000" verticalHuggingPriority="751" horizontalCompressionResistancePriority="1000" verticalCompressionResistancePriority="751" text="Hello, world!" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="aKX-7S-WBY"> | |
<rect key="frame" x="107.66666666666667" y="33.333333333333314" width="177.66666666666663" height="40.666666666666657"/> | |
<fontDescription key="fontDescription" style="UICTFontTextStyleTitle0"/> | |
<nil key="textColor"/> | |
<nil key="highlightedColor"/> | |
</label> | |
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="1000" verticalHuggingPriority="751" horizontalCompressionResistancePriority="1000" verticalCompressionResistancePriority="751" text="Hello, world!" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="PFp-5d-qnS"> | |
<rect key="frame" x="123" y="74" width="147" height="33.666666666666657"/> | |
<fontDescription key="fontDescription" style="UICTFontTextStyleTitle1"/> | |
<color key="textColor" systemColor="tintColor"/> | |
<nil key="highlightedColor"/> | |
</label> | |
</subviews> | |
</stackView> | |
</subviews> | |
</stackView> | |
</subviews> | |
<viewLayoutGuide key="safeArea" id="fnl-2z-Ty3"/> | |
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> | |
<constraints> | |
<constraint firstItem="fnl-2z-Ty3" firstAttribute="trailing" secondItem="Jah-Wg-omT" secondAttribute="trailing" id="93d-y5-W2L"/> | |
<constraint firstItem="Jah-Wg-omT" firstAttribute="top" secondItem="fnl-2z-Ty3" secondAttribute="top" id="NCi-G3-oGf"/> | |
<constraint firstAttribute="bottom" secondItem="Jah-Wg-omT" secondAttribute="bottom" id="QdY-Em-Ojt"/> | |
<constraint firstItem="Jah-Wg-omT" firstAttribute="leading" secondItem="fnl-2z-Ty3" secondAttribute="leading" id="Rf3-Vz-UFm"/> | |
</constraints> | |
<point key="canvasLocation" x="27" y="20"/> | |
</view> | |
</objects> | |
<resources> | |
<image name="rainbow" catalog="system" width="128" height="71"/> | |
<systemColor name="tintColor"> | |
<color red="0.0" green="0.47843137254901963" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> | |
</systemColor> | |
</resources> | |
</document> |
Author
Koshimizu-Takehito
commented
Oct 11, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment