Skip to content

Commit 555e592

Browse files
authored
fix(ios-searchbar): fix searchbar auto sizing in iOS11 (NativeScript#5658)
1 parent 893cbb4 commit 555e592

3 files changed

Lines changed: 49 additions & 4 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Page xmlns="http://schemas.nativescript.org/tns.xsd">
2+
<StackLayout>
3+
<GridLayout height="60" rows="auto" columns="auto, *">
4+
<SearchBar id="searchBar1" hint="Search" text="auto(sb), * col, no width" />
5+
</GridLayout>
6+
<GridLayout height="60" rows="auto" columns="auto, *">
7+
<SearchBar id="searchBar2" width="100%" hint="Search" text="auto(sb), * col, width 100%" />
8+
</GridLayout>
9+
<GridLayout height="60" rows="auto" columns="auto, *">
10+
<SearchBar id="searchBar3" width="300" hint="Search" text="auto(sb), * col, width 300dip" />
11+
</GridLayout>
12+
<GridLayout height="60" rows="auto" columns="*, auto">
13+
<SearchBar col="1" id="searchBar4" width="300" hint="Search" text="*, auto(sb) col, width 300dip" />
14+
</GridLayout>
15+
<GridLayout height="60" rows="auto" columns="auto, 200">
16+
<SearchBar id="searchBar5" hint="Search" text="auto(sb), 200 cols, no width" />
17+
</GridLayout>
18+
<GridLayout height="60" rows="auto" columns="200, auto">
19+
<SearchBar col="1" id="searchBar6" hint="Search" text="200, auto(sb) cols, no width" />
20+
</GridLayout>
21+
<StackLayout height="60">
22+
<SearchBar id="searchBar7" hint="Search" text="stack, no width" />
23+
</StackLayout>
24+
<StackLayout height="60">
25+
<SearchBar id="searchBar8" width="300" hint="Search" text="stack, width 300dip" />
26+
</StackLayout>
27+
</StackLayout>
28+
</Page>

apps/app/ui-tests-app/search-bar/main-page.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ export function pageLoaded(args: EventData) {
1111

1212
export function loadExamples() {
1313
const examples = new Map<string, string>();
14-
examples.set("issue-4147", "search-bar/issue-4147");
15-
examples.set("search-bar", "search-bar/search-bar");
16-
examples.set("issue-5039","search-bar/issue-5039");
14+
examples.set("issue-4147", "search-bar/issue-4147");
15+
examples.set("search-bar", "search-bar/search-bar");
16+
examples.set("issue-5039", "search-bar/issue-5039");
17+
examples.set("issue-5655", "search-bar/issue-5655");
18+
1719
return examples;
1820
}

tns-core-modules/ui/search-bar/search-bar.ios.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import {
33
SearchBarBase, Color, colorProperty, backgroundColorProperty, backgroundInternalProperty, fontInternalProperty,
44
textProperty, hintProperty, textFieldHintColorProperty, textFieldBackgroundColorProperty
55
} from "./search-bar-common";
6+
import { ios as iosUtils } from "../../utils/utils";
67

78
export * from "./search-bar-common";
89

10+
const majorVersion = iosUtils.MajorVersion;
11+
912
class UISearchBarDelegateImpl extends NSObject implements UISearchBarDelegate {
1013
public static ObjCProtocols = [UISearchBarDelegate];
1114

@@ -52,6 +55,18 @@ class UISearchBarDelegateImpl extends NSObject implements UISearchBarDelegate {
5255
}
5356
}
5457

58+
class UISearchBarImpl extends UISearchBar {
59+
sizeThatFits(size: CGSize): CGSize {
60+
// iOS11 SDK does not support passing sizeThatFits(...) non-finite width value;
61+
// iOS layout system will take care to size the element properly when passed 0
62+
if (majorVersion >= 11 && size.width === Number.POSITIVE_INFINITY) {
63+
size.width = 0;
64+
}
65+
66+
return super.sizeThatFits(size);
67+
}
68+
}
69+
5570
export class SearchBar extends SearchBarBase {
5671
private _ios: UISearchBar;
5772
private _delegate;
@@ -61,7 +76,7 @@ export class SearchBar extends SearchBarBase {
6176
constructor() {
6277
super();
6378

64-
this.nativeViewProtected = this._ios = UISearchBar.new();
79+
this.nativeViewProtected = this._ios = UISearchBarImpl.new();
6580
this._delegate = UISearchBarDelegateImpl.initWithOwner(new WeakRef(this));
6681
}
6782

0 commit comments

Comments
 (0)