flutter_flutter/dev/integration_tests/ios_host_app/Host/DynamicResizingViewController.m
LouiseHsu 1bf445bb45
[ios] Reland Dynamic Content Resizing (#179153)
This is a reland of https://github.com/flutter/flutter/pull/177410

The previous pr was hitting deadlocks with tests that use screenshots.
This reland fixes it by submitting frames asynchronously instead of
synchronously.

This PR fixes https://github.com/flutter/flutter/issues/169147

This is the iOS implementation of
https://github.com/flutter/flutter/issues/149033

In Add-to-App scenarios, a common desired use case is to have the
embedded FlutterView be able to size itself based on it's content. Up
till now, the size had to be manually specified, which can be tedious
and inaccurate.

This PR adds a new flag, `isAutoResizable`, which enables the ability
for the embedded flutter view to dynamically set it's own size based off
it's content. Note that this feature will *NOT* work if the the embedded
app is wrapped in flexible or unsized widgets, such as Scaffold. The
FlutterView will just not display at all.

To use the new flag, just set it like
```
flutterViewController.autoResizable = true;
```
or in Swift

```
flutterViewController.isAutoResizable = true
```
## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.
2025-12-02 18:59:44 +00:00

71 lines
2.9 KiB
Objective-C

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Flutter/Flutter.h>
#import "DynamicResizingViewController.h"
@interface DynamicResizingViewController ()
@end
@implementation DynamicResizingViewController {}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor systemBackgroundColor];
self.title = @"Dynamic Content Resizing";
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:scrollView];
UIStackView *stackView = [[UIStackView alloc] init];
stackView.translatesAutoresizingMaskIntoConstraints = NO;
stackView.axis = UILayoutConstraintAxisVertical;
stackView.spacing = 10;
[scrollView addSubview:stackView];
[NSLayoutConstraint activateConstraints:@[
[scrollView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor],
[scrollView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
[scrollView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
[scrollView.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor]
]];
[NSLayoutConstraint activateConstraints:@[
[stackView.topAnchor constraintEqualToAnchor:scrollView.contentLayoutGuide.topAnchor],
[stackView.leadingAnchor constraintEqualToAnchor:scrollView.contentLayoutGuide.leadingAnchor],
[stackView.trailingAnchor constraintEqualToAnchor:scrollView.contentLayoutGuide.trailingAnchor],
[stackView.bottomAnchor constraintEqualToAnchor:scrollView.contentLayoutGuide.bottomAnchor],
[stackView.widthAnchor constraintEqualToAnchor:scrollView.frameLayoutGuide.widthAnchor]
]];
for (int index = 1; index <= 50; index++) {
if (index == 10) {
_flutterViewController = [[FlutterViewController alloc] init];
[_flutterViewController setInitialRoute:@"resize"];
_flutterViewController.autoResizable = YES;
[self addChildViewController:_flutterViewController];
[stackView addArrangedSubview:_flutterViewController.view];
_flutterViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
_flutterViewController.view.accessibilityIdentifier = @"flutter_view";
[_flutterViewController didMoveToParentViewController:self];
} else {
UILabel *label = [[UILabel alloc] init];
label.text = [NSString stringWithFormat:@" Hello from iOS %d ", index];
label.backgroundColor = (index % 2 == 0) ? [UIColor systemGray5Color] : [UIColor systemGray3Color];
label.translatesAutoresizingMaskIntoConstraints = NO;
[label.heightAnchor constraintEqualToConstant:44].active = YES;
[stackView addArrangedSubview:label];
}
}
}
@end