Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add headerTopInsetEnabled prop that defaults to true to account for translucent status bars on Android #545

Merged
merged 4 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
Expand Down Expand Up @@ -35,6 +36,7 @@ public class ScreenStackHeaderConfig extends ViewGroup {
private boolean mIsShadowHidden;
private boolean mDestroyed;
private boolean mBackButtonInCustomView;
private boolean mIsTopInsetEnabled = true;
private int mTintColor;
private final Toolbar mToolbar;

Expand Down Expand Up @@ -153,6 +155,19 @@ public void onUpdate() {
getScreenFragment().setToolbar(mToolbar);
}

if (mIsTopInsetEnabled) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mToolbar.setPadding(0, getRootWindowInsets().getSystemWindowInsetTop(), 0, 0);
} else {
// Hacky fallback for old android. Before Marshmallow, the status bar height was always 25
mToolbar.setPadding(0, (int) (25 * getResources().getDisplayMetrics().density), 0, 0);
}
} else {
if (mToolbar.getPaddingTop() > 0) {
mToolbar.setPadding(0, 0, 0, 0);
}
}

activity.setSupportActionBar(mToolbar);
ActionBar actionBar = activity.getSupportActionBar();

Expand Down Expand Up @@ -322,6 +337,8 @@ public void setTintColor(int color) {
mTintColor = color;
}

public void setTopInsetEnabled(boolean topInsetEnabled) { mIsTopInsetEnabled = topInsetEnabled; }

public void setBackgroundColor(int color) {
mBackgroundColor = color;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public void setHideBackButton(ScreenStackHeaderConfig config, boolean hideBackBu
config.setHideBackButton(hideBackButton);
}

@ReactProp(name = "topInsetEnabled")
public void setTopInsetEnabled(ScreenStackHeaderConfig config, boolean topInsetEnabled) {
config.setTopInsetEnabled(topInsetEnabled);
}

@ReactProp(name = "color", customType = "Color")
public void setColor(ScreenStackHeaderConfig config, int color) {
config.setTintColor(color);
Expand Down
4 changes: 4 additions & 0 deletions native-stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ Function which returns a React Element to display in the center of the header.

Boolean indicating whether the navigation bar is translucent. Only supported on iOS.

#### `headerTopInsetEnabled`

A Boolean to that lets you opt out of insetting the header. You may want to * set this to `false` if you use an opaque status bar. Defaults to `true`. Insets are always applied on iOS because the header cannot be opaque. Only supported on Android.

#### `headerLargeTitle`

Boolean used to set a native property to prefer a large title header (like in iOS setting).
Expand Down
9 changes: 9 additions & 0 deletions src/native-stack/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ export type NativeStackNavigationOptions = {
fontFamily?: string;
fontSize?: number;
};
/**
* A flag to that lets you opt out of insetting the header. You may want to
* set this to `false` if you use an opaque status bar. Defaults to `true`.
* Only supported on Android. Insets are always applied on iOS because the
* header cannot be opaque.
*
* @platform android
*/
headerTopInsetEnabled?: boolean;
/**
* Style object for the scene content.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/native-stack/views/HeaderConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default function HeaderConfig(props: Props) {
headerHideShadow,
headerLargeTitleHideShadow,
headerTintColor,
headerTopInsetEnabled = true,
headerLargeTitle,
headerTranslucent,
headerStyle = {},
Expand Down Expand Up @@ -64,6 +65,7 @@ export default function HeaderConfig(props: Props) {
? headerTintColor
: colors.text
}
topInsetEnabled={headerTopInsetEnabled}
backTitle={headerBackTitleVisible ? headerBackTitle : ' '}
backTitleFontFamily={headerBackTitleStyle.fontFamily}
backTitleFontSize={headerBackTitleStyle.fontSize}
Expand Down