From 067549864fb64ade66c83787ea2353c6c31007f8 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sat, 28 Nov 2026 05:03:28 +0000 Subject: [PATCH] feat: add apportionsSegmentWidthsByContent prop for iOS Expose UISegmentedControl's `apportionsSegmentWidthsByContent` so the width of each segment can follow the width of its content instead of every segment getting the same width. This keeps short labels compact and gives long labels room when horizontal space is limited. - iOS: export the prop from the view manager; it maps directly onto the UISegmentedControl property, so no extra native code is needed. - Add the prop to the Flow and TypeScript typings, with a type-level test. - Document it in the README as iOS-only and add it to the example app. Fixes #883 --- README.md | 10 ++++++++++ example/App.js | 9 +++++++++ index.d.ts | 7 +++++++ ios/RNCSegmentedControlManager.m | 1 + js/types.js | 6 ++++++ types-test/typecheck.tsx | 8 ++++++++ 6 files changed, 41 insertions(+) diff --git a/README.md b/README.md index dc2fdcd4..8041241d 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,16 @@ If true, then selecting a segment won't persist visually. The `onValueChange` ca --- +### `apportionsSegmentWidthsByContent` + +If true, the width of each segment is proportional to the width of its content, so short labels take less room than long ones. By default all segments have the same width. Maps to `UISegmentedControl`'s [`apportionsSegmentWidthsByContent`](https://developer.apple.com/documentation/uikit/uisegmentedcontrol/apportionssegmentwidthsbycontent). + +| Type | Required | Platform | +| ---- | -------- | -------- | +| bool | No | iOS | + +--- + ### `onChange` Callback that is called when the user taps a segment; passes the event as an argument diff --git a/example/App.js b/example/App.js index 68a108e5..29f1bbd4 100644 --- a/example/App.js +++ b/example/App.js @@ -65,6 +65,15 @@ const App = () => { + + + Segment widths can follow their content (iOS) + + + Segmented controls can be disabled diff --git a/index.d.ts b/index.d.ts index 00dec52f..00dcfbef 100644 --- a/index.d.ts +++ b/index.d.ts @@ -97,6 +97,13 @@ export interface SegmentedControlProps extends ViewProps { */ momentary?: boolean; + /** + * (iOS only) + * If true, the width of each segment is proportional to the width of its + * content instead of all segments sharing the same width. + */ + apportionsSegmentWidthsByContent?: boolean; + /** * Callback that is called when the user taps a segment; * passes the event as an argument diff --git a/ios/RNCSegmentedControlManager.m b/ios/RNCSegmentedControlManager.m index bc899cae..2234a545 100644 --- a/ios/RNCSegmentedControlManager.m +++ b/ios/RNCSegmentedControlManager.m @@ -29,6 +29,7 @@ - (UIView *)view { RCT_EXPORT_VIEW_PROPERTY(appearance, NSString) RCT_EXPORT_VIEW_PROPERTY(testIDS, NSArray) RCT_EXPORT_VIEW_PROPERTY(disabledIndices, NSArray) +RCT_EXPORT_VIEW_PROPERTY(apportionsSegmentWidthsByContent, BOOL) RCT_CUSTOM_VIEW_PROPERTY(fontStyle, NSObject, RNCSegmentedControl) { #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ diff --git a/js/types.js b/js/types.js index 898dfe1c..52eca1d3 100644 --- a/js/types.js +++ b/js/types.js @@ -94,6 +94,12 @@ export type SegmentedControlProps = Readonly<{| * The `onValueChange` callback will still work as expected. */ momentary?: ?boolean, + /** + * (iOS only) + * If true, the width of each segment is proportional to the width of its + * content instead of all segments sharing the same width. + */ + apportionsSegmentWidthsByContent?: ?boolean, /** * (iOS 13+ only, Android, Web) * Overrides the control's appearance irrespective of the OS theme diff --git a/types-test/typecheck.tsx b/types-test/typecheck.tsx index ad55feba..fb069ef4 100644 --- a/types-test/typecheck.tsx +++ b/types-test/typecheck.tsx @@ -55,4 +55,12 @@ export const ImageValues = () => ( /> ); +// #883: segment widths that follow their content (iOS). +export const ContentWidths = () => ( + +); + declare function require(path: string): number;