Skip to content

Commit d22877f

Browse files
committed
works
1 parent 10aa6f7 commit d22877f

2 files changed

Lines changed: 84 additions & 45 deletions

File tree

add_to_app/ios_content_resizing/ios_content_resizing/ios_content_resizing/ViewController.swift

Lines changed: 84 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,93 @@ import Flutter
22
import UIKit
33

44
class ViewController: UIViewController {
5-
override func viewDidLoad() {
6-
super.viewDidLoad()
7-
8-
showScollView()
9-
}
10-
11-
func showScollView() {
12-
let scrollView = UIScrollView()
13-
scrollView.isUserInteractionEnabled = true
5+
override func viewDidLoad() {
6+
super.viewDidLoad()
7+
view.backgroundColor = .systemGroupedBackground
8+
showStyledScrollView()
9+
}
1410

15-
let stackView = UIStackView()
16-
stackView.axis = .vertical
17-
stackView.distribution = .fill
18-
stackView.backgroundColor = .yellow
19-
stackView.translatesAutoresizingMaskIntoConstraints = false
11+
func showStyledScrollView() {
12+
let scrollView = UIScrollView()
13+
let stackView = UIStackView()
14+
stackView.axis = .vertical
15+
stackView.spacing = 12 // Spacing between the "cards"
2016

21-
let engine1 = FlutterEngine(name: "one")
22-
engine1.run()
17+
let engine1 = FlutterEngine(name: "one")
18+
engine1.run()
2319

24-
for index in 1...50 {
25-
if (index == 10) {
26-
let flutterViewController = FlutterViewController(engine: engine1, nibName: nil, bundle: nil)
27-
flutterViewController.isAutoResizable = true
28-
addChild(flutterViewController)
29-
stackView.addArrangedSubview(flutterViewController.view)
30-
flutterViewController.didMove(toParent: self)
20+
for index in 1...50 {
21+
if index == 10 {
22+
// This is our Flutter view
23+
let flutterViewController = FlutterViewController(engine: engine1, nibName: nil, bundle: nil)
24+
flutterViewController.isAutoResizable = true
25+
addChild(flutterViewController)
26+
27+
// The Flutter view's background should be clear to see the card behind it
28+
flutterViewController.view.backgroundColor = .clear
29+
30+
// Wrap the Flutter view in a styled container "card"
31+
let cardView = UIView()
32+
cardView.backgroundColor = .systemBackground
33+
cardView.layer.cornerRadius = 12
34+
cardView.layer.masksToBounds = true
35+
36+
flutterViewController.view.translatesAutoresizingMaskIntoConstraints = false
37+
cardView.addSubview(flutterViewController.view)
38+
39+
NSLayoutConstraint.activate([
40+
flutterViewController.view.topAnchor.constraint(equalTo: cardView.topAnchor),
41+
flutterViewController.view.leadingAnchor.constraint(equalTo: cardView.leadingAnchor),
42+
flutterViewController.view.trailingAnchor.constraint(equalTo: cardView.trailingAnchor),
43+
flutterViewController.view.bottomAnchor.constraint(equalTo: cardView.bottomAnchor)
44+
])
45+
46+
stackView.addArrangedSubview(cardView)
47+
flutterViewController.didMove(toParent: self)
3148

32-
} else {
33-
let label = UILabel()
34-
label.text = "Hello from iOS \(index)"
35-
stackView.addArrangedSubview(label)
36-
}
49+
} else {
50+
let label = UILabel()
51+
label.text = "It's me, iOS \(index)"
52+
label.translatesAutoresizingMaskIntoConstraints = false
53+
54+
let cardView = UIView()
55+
cardView.backgroundColor = .systemBackground
56+
cardView.layer.cornerRadius = 12
57+
cardView.addSubview(label)
58+
59+
NSLayoutConstraint.activate([
60+
label.topAnchor.constraint(equalTo: cardView.topAnchor, constant: 16),
61+
label.bottomAnchor.constraint(equalTo: cardView.bottomAnchor, constant: -16),
62+
label.leadingAnchor.constraint(equalTo: cardView.leadingAnchor, constant: 16),
63+
label.trailingAnchor.constraint(equalTo: cardView.trailingAnchor, constant: -16)
64+
])
65+
66+
stackView.addArrangedSubview(cardView)
67+
}
68+
}
69+
70+
// --- Auto Layout Setup ---
71+
scrollView.translatesAutoresizingMaskIntoConstraints = false
72+
stackView.translatesAutoresizingMaskIntoConstraints = false
73+
74+
view.addSubview(scrollView)
75+
scrollView.addSubview(stackView)
76+
77+
// Set constraints for the scroll view to fill the main view
78+
NSLayoutConstraint.activate([
79+
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
80+
scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
81+
scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
82+
scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
83+
])
84+
85+
// Set constraints for the stack view within the scroll view
86+
NSLayoutConstraint.activate([
87+
stackView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor, constant: 20),
88+
stackView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor, constant: -20),
89+
// Inset the stack view horizontally for the grouped look
90+
stackView.leadingAnchor.constraint(equalTo: scrollView.frameLayoutGuide.leadingAnchor, constant: 20),
91+
stackView.trailingAnchor.constraint(equalTo: scrollView.frameLayoutGuide.trailingAnchor, constant: -20)
92+
])
3793
}
38-
scrollView.addSubview(stackView)
39-
scrollView.layoutIfNeeded()
40-
self.view.addSubview(scrollView)
41-
42-
scrollView.translatesAutoresizingMaskIntoConstraints = false
43-
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
44-
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
45-
scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
46-
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
47-
48-
stackView.translatesAutoresizingMaskIntoConstraints = false
49-
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
50-
stackView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
51-
stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
52-
stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
53-
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
54-
}
5594
}

0 commit comments

Comments
 (0)