Modal组件可以用来覆盖包含React Native根视图的原生视图(如UIViewController,Activity)。
在嵌入React Native的混合应用中可以使用Modal。Modal可以使你应用中RN编写的那部分内容覆盖在原生视图上显示。import React, { Component } from 'react';
import React, { Component } from 'react';
import { Modal, Text, TouchableHighlight, View ,AppRegistry} from 'react-native';
class ModalExample extends Component {
constructor(props) {
super(props);
this.state = {modalVisible: true};
}
setModalVisible(visible) {
this.setState({modalVisible: visible});
}
render() {
return (
<View style={{marginTop: 22}}>
<Modal
animationType={"fade"}
transparent={false}
visible={this.state.modalVisible}
onRequestClose={() => {alert("Modal has been closed.")}}
>
<View style={{marginTop: 22}}>
<View>
<Text>Hello World!</Text>
<TouchableHighlight onPress={() => {
this.setModalVisible(!this.state.modalVisible)
}}>
<Text>Hide Modal</Text>
</TouchableHighlight>
</View>
</View>
</Modal>
<TouchableHighlight onPress={() => {
this.setModalVisible(true)
}}>
<Text>Show Modal</Text>
</TouchableHighlight>
</View>
);
}
}
AppRegistry.registerComponent('TestModal', () => ModalExample);
NSURL *jsCodeLocation;
//
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"TestModal" fallbackResource:nil];
// NSURL *jsCodeLocation = [NSURL
// URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios"];
// RCTRootView *rootView =
// [[RCTRootView alloc] initWithBundleURL : jsCodeLocation
// moduleName : @"TextActivity"
// initialProperties :
// @{
// @"scores" : @[
// @{
// @"name" : @"Alex",
// @"value": @"42"
// },
// @{
// @"name" : @"Joel",
// @"value": @"10"
// }
// ]
// }
// launchOptions : nil];
RCTRootView *rootView =
[[RCTRootView alloc] initWithBundleURL : jsCodeLocation
moduleName : @"TestModal"
initialProperties :nil launchOptions : nil];
// UIViewController *vc = [[UIViewController alloc] init];
// vc.view = rootView;
// [self presentViewController:vc animated:YES completion:nil];
[self.view addSubview:rootView];
animationType PropTypes.oneOf(['none', 'slide', 'fade'])
The animationType
prop controls how the modal animates.
slide
slides in from the bottomfade
fades into viewnone
appears without an animation
onRequestClose Platform.OS === 'android' ? PropTypes.func.isRequired : PropTypes.func
The onRequestClose
prop allows passing a function that will be called once the modal has been dismissed.
On the Android platform, this is a required function.
onShow function
The onShow
prop allows passing a function that will be called once the modal has been shown.
transparent bool
The transparent
prop determines whether your modal will fill the entire view. Setting this to true
will render the modal over a transparent background.
onOrientationChange PropTypes.func
The onOrientationChange
callback is called when the orientation changes while the modal is being displayed. The orientation provided is only 'portrait' or 'landscape'. This callback is also called on initial render, regardless of the current orientation.
supportedOrientations PropTypes.arrayOf(PropTypes.oneOf(['portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right']))
The supportedOrientations
prop allows the modal to be rotated to any of the specified orientations. On iOS, the modal is still restricted by what's specified in your app's Info.plist's UISupportedInterfaceOrientations field.