React Native 适配iPhone X

本文介绍如何在React项目中使用react-navigation实现跨平台一致的Header样式,并自定义Tab栏,包括配置不同屏幕尺寸下的样式调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中,使用react-navigation和自定义Header,这样保证iOS和安卓显示的Header显示一样

navigation的头部高度为,sizeHeader=84,
头部内容为,sizeHeaderContent=44,
解决屏幕的高度问题,这时候直接在AppDelegate中实现
self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-34)];

自定义Header

import React, { Component } from 'react';
import {
    StyleSheet,
    View,
    Dimensions,
    Image,
    StatusBar,
    TouchableOpacity,
    Platform,
} from 'react-native';

import { Widget } from 'rn-yunxi';
const { width, height } = Dimensions.get('window');
const BackIcon = require('./back.png');
const Text = Widget.Text;
//模块声名并导出
export default class Header extends Component {
    //属性声名
    static propTypes = {
        navigation: React.PropTypes.any,
        showBackAction: React.PropTypes.bool,
        backAction: React.PropTypes.func,
        title: React.PropTypes.string,
        defaultStatusBar: React.PropTypes.bool,
        rightTitle: React.PropTypes.string,
        rightAction: React.PropTypes.func,
        rightIcon: React.PropTypes.element,
        containerStyles:React.PropTypes.any,
        headerViewStyles:React.PropTypes.any,
        backIconSrc:React.PropTypes.number,
        showDivider: React.PropTypes.bool

    };
    //默认属性
    static defaultProps = {
        title: '',
        defaultStatusBar: true,
        showBackAction: true,
        showDivider: true


    };
    //构造函数
    constructor(props) {
        super(props);
        this.state = { //状态机变量声明
        }
    }

    //渲染
    render() {
        return (
            <View style={[styles.container,this.props.containerStyles]}>
                <View style={[styles.headerView,this.props.headerViewStyles]}>
                    {Platform.OS === 'ios' ? <StatusBar barStyle={this.props.defaultStatusBar ? 'default' : 'light-content'} /> : null}
                    <View style={styles.header}>
                        <View style={styles.titleView}>
                            <Text style={styles.title}>{this.props.title}</Text>
                        </View>
                        {this.props.showBackAction ? <TouchableOpacity
                            onPress={() => {
                                if (this.props.backAction) {
                                    this.props.backAction();

                                } else {
                                    if (this.props.navigation) {
                                        console.log(this.props.navigation);
                                        this.props.navigation.goBack();
                                    }
                                }
                            }}>
                            <View style={styles.backView}>
                                <Image style={{width:22,height:22}} source={this.props.backIconSrc?this.props.backIconSrc:BackIcon} />
                            </View>
                        </TouchableOpacity> : <View />}


                        {this.props.rightTitle ? <TouchableOpacity onPress={() => {
                            if (this.props.rightAction) {
                                this.props.rightAction();
                            }
                        }}>
                            <View style={styles.rightView}>
                                <Text style={this.props.rightTextStyle}>{this.props.rightTitle}</Text>
                            </View>
                        </TouchableOpacity> : null}

                            {this.props.rightIcon}

                    </View>
                </View>

                {
                    this.props.showDivider == true ?   <View style={GlobalStyle.styleDividerDefault} /> : null
                }
                {
                    this.props.children
                }
            </View>
        );
    }
};

  const sizeHeader = (Platform.OS === 'ios'&&  ISIPHONEX)?84:Platform.OS === 'ios'?64: 50;
  const sizeHeaderMarginTop = Platform.OS === 'ios' ? ISIPHONEX ?35:20: 0
  const sizeHeaderContent = Platform.OS === 'ios' ? 44 : 50;
//iPhone X
  const ISIPHONEX = Dimensions.get('window').width==375 &&        Dimensions.get('window').height == 812

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F3F3F3',
        width: width,
        height: height,
    },

    headerView: {
        backgroundColor: '#FFFFFF',
        height: sizeHeader,
    },
    header: {
        justifyContent: 'space-between',
        width: width,
        marginTop:sizeHeaderMarginTop,
        flexDirection: 'row',
        height: sizeHeaderContent,
        alignItems:'center',
        paddingRight:15
    },
    titleView: {
        left: 0,
        top: 0,
        height: sizeHeaderContent,
        position: 'absolute',
        width: width,
        justifyContent: 'center',
        alignItems: 'center'
    },
    title: {
        fontSize: 18,
    },
    backView: {
        justifyContent: 'center',
        paddingLeft: 10,
        width: 50,
        flex: 1,
    },
    rightView: {
        flex: 1,
        justifyContent: 'center',
        paddingRight: 10,
        alignItems: 'flex-end',
        minWidth: 50
    }
});

AppDelegate.m

@property (nonatomic, strong) UIWindow *window;
@property (nonatomic,strong) UIViewController * rootViewController;

#define is_iPhoneX [UIScreen mainScreen].bounds.size.width == 375.0f && [UIScreen mainScreen].bounds.size.height == 812.0f   //判断是否iPhoneX

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 

   self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
   _rootViewController = [UIViewController new];
   _rootViewController.view = rootView;
   self.window.rootViewController = _rootViewController;
   [self.window makeKeyAndVisible];
}

react-navigation的配置:


const renderTabImage = (image) => {
    return (
        <Image style={[{ width: Constant.scale(21), height: Constant.scale(21) }]} resizeMode="contain" source={image} />
    )

};

const TabStack = TabNavigator(
    {
        Tab1: {
            title: '首页',
            login: true,
            screen: HomePage,
            navigationOptions: {
                header: null,
                tabBarLabel: '首页',
                tabBarIcon: ({ focused, tintColor }) => (
                    renderTabImage(!focused ? IMG_HOME : IMG_HOME_SELECTED)
                ),

            }
        },

        Tab2: {
            title: '产品目录',
            login: true,
            screen: ProductSummary,
            navigationOptions: {
                header: null,
                tabBarLabel: '产品目录',
                tabBarIcon: ({ focused, tintColor }) => (
                    renderTabImage(!focused ? IMG_PM : IMG_PM_SELECTED)
                ),

            }
        },
        Tab3: {
            title: '加入如新',
            login: true,
            screen: MyIdentity,
            navigationOptions: {
                header: null,
                tabBarLabel: '加入如新',
                tabBarIcon: ({ focused, tintColor }) => (
                    renderTabImage(!focused ? IMG_ADDNUSKIN : IMG_ADDNUSKIN_SELECTED)
                ),

            }
        },

        Tab4: {
            title: '个人中心',
            login: true,
            screen: MinePage,
            navigationOptions: {
                header: null,
                tabBarLabel: '个人中心',
                tabBarIcon: ({ focused, tintColor }) => (
                    renderTabImage(!focused ? IMG_MINE : IMG_MINE_SELECTED)
                ),

            }
        },

    },
    {
        tabBarVisible: false,
        tabBarPosition: 'bottom',
        backBehavior: 'none',
        swipeEnabled: false,
        animationEnabled: false,
        lazy: true,
        tabBarOptions: {
            showIcon: true,
            scrollEnabled: false,
            activeTintColor: Constant.colorPrimary,
            inactiveTintColor: Constant.colorTxtDefault,
            style: {
                margin: 0,
                backgroundColor: 'white',
                paddingTop: 0,
                height:  ISIPHONEX ?  Constant.scale(74) : Constant.scale(50),
                // paddingTop
            },
            tabStyle: {
                borderTopWidth: Constant.sizeDividerNormal,
                borderTopColor: Constant.colorDivider,
                margin: 0,
                padding: 0,
            },
            indicatorStyle: {
                height: 0
            },
            labelStyle: {
                padding: 0,
                margin: 0,
                marginTop: 0,
                fontSize: 12,
                marginBottom: ISIPHONEX ? 30 : 6,
            },

        }
    }
);

const TabRuXin = TabNavigator(
    {
        Tab1: {
            title: '首页',
            login: true,
            screen: HomePage,
            navigationOptions: {
                header: null,
                tabBarLabel: '首页',
                tabBarIcon: ({ focused, tintColor }) => (
                    renderTabImage(!focused ? IMG_HOME : IMG_HOME_SELECTED)
                ),

            }
        },

        Tab2: {
            title: '产品目录',
            login: true,
            screen: ProductSummary,
            navigationOptions: {
                header: null,
                tabBarLabel: '产品目录',
                tabBarIcon: ({ focused, tintColor }) => (
                    renderTabImage(!focused ? IMG_PM : IMG_PM_SELECTED)
                ),

            }
        },
        Tab3: {
            title: '我的事业',
            login: true,
            screen: MyCareer,
            navigationOptions: {
                header: null,
                tabBarLabel: '我的事业',
                tabBarIcon: ({ focused, tintColor }) => (
                    renderTabImage(!focused ? IMG_ADDNUSKIN : IMG_ADDNUSKIN_SELECTED)
                ),

            }
        },

        Tab4: {
            title: '个人中心',
            login: true,
            screen: MinePage,
            navigationOptions: {
                header: null,
                tabBarLabel: '个人中心',
                tabBarIcon: ({ focused, tintColor }) => (
                    renderTabImage(!focused ? IMG_MINE : IMG_MINE_SELECTED)
                ),

            }
        },

    },
    {
        tabBarVisible: false,
        tabBarPosition: 'bottom',
        backBehavior: 'none',
        swipeEnabled: false,
        animationEnabled: false,
        lazy: true,
        tabBarOptions: {
            showIcon: true,
            scrollEnabled: false,
            activeTintColor: Constant.colorPrimary,
            inactiveTintColor: Constant.colorTxtDefault,
            style: {
                margin: 0,
                backgroundColor: 'white',
                paddingTop: 0,
                height: Constant.scale(50),
            },
            tabStyle: {
                borderTopWidth: Constant.sizeDividerNormal,
                borderTopColor: Constant.colorDivider,
                margin: 0,
                padding: 0,
            },
            indicatorStyle: {
                height: 0
            },
            labelStyle: {
                padding: 0,
                margin: 0,
                marginTop: 1,
                fontSize: Constant.scale(11),
            },

        }
    }
);

Header的使用

  render() {
        return (
                <Header title={'库存查询'} navigation={this.props.navigation}>
                </Header>
        );
    }

这里写图片描述

### 关于 Qt 在麒麟和鸿蒙系统中的兼容性 Qt 是一种跨平台的 C++ 图形用户界面库,其强大的开放性和灵活性使其能够适应多种操作系统和硬件环境。对于国产操作系统如麒麟(Kylin)和鸿蒙HarmonyOS),Qt 的兼容性主要取决于这些系统的底层架构和技术实现。 #### 麒麟操作系统的兼容性分析 麒麟操作系统基于 Linux 内核构建,而 Android 底层使用的也是经过深度定制的 Linux 平台[^3]。由于 Qt 提供了良好的技术支持并具有高度的可移植性,在这种环境下配置和移植 Qt 成为了可能。因此,理论上 Qt 可以很好地适配麒麟操作系统,并提供图形化界面的支持。 #### 鸿蒙操作系统的支持情况探讨 鸿蒙不仅仅是一个传统意义上的单一操作系统,它还代表了一个完整的生态系统[^2]。这个系统旨在覆盖从智能手机到智能家居的各种设备类型。关于 Qt 对鸿蒙的具体支持状况: - **技术可行性**:如果鸿蒙在其某些版本或者特定设备上保留了类 Unix 或者 POSIX 兼容层,则 Qt 很有可能通过调整编译参数等方式来完成部署。 - **实际案例与文档缺乏**:目前公开资料中并未明确提到 Qt 已经被正式集成进入鸿蒙官方 SDK 或工具链之中[^4]。不过考虑到两者都追求广泛的软硬件协同工作能力,未来或许会有更多合作机会出现。 另外值得注意的是,无论是麒麟还是鸿蒙,它们均强调自身的 **可裁剪性** 和 **灵活配置** 特征[^5]。这意味着即使当前存在一定的局限性,随着技术进步及市场需求变化,针对不同应用场景优化后的解决方案也可能逐步推出。 ```cpp // 示例代码展示如何检测目标平台上是否存在POSIX标准函数 #include <unistd.h> #include <iostream> int main() { if (isatty(STDOUT_FILENO)) { // 判断是否连接至终端 std::cout << "Running on a system with POSIX compatibility." << std::endl; } else { std::cerr << "No direct terminal access detected!" << std::endl; } } ``` 上述简单测试可以帮助初步判断某个未知环境中是否有足够的基础接口用于进一步开发基于 Qt 的应用程序。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值