最近需要做个平板的项目,然后需要直接横屏,有2种实现方式。
1,随着屏幕旋转,布局自动调整。做横竖屏适配
2,强制屏幕横屏,不随着屏幕去调整
第一种方式这里就不做说明了。代码做适配就可以。下面说一下第二种实现方式
颤振为我们提供了方法来控制系统的横竖屏显示
SystemChrome.setPreferredOrientations ([ DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, DeviceOrientation.portraitUp, DeviceOrientation.portraitDown ))((_){ });
包含的方向类型! ! ! !但是但是但是这个方法只适用于android在iOS上没有效果,上网查资料有大神封装的颤振插件定位、颤振插件auto_orientation在iOS上都起不到效果,所以打算自己写一个原生文件与颤振进行通讯,实现屏幕旋转。也是笔者在查询资料之后做的一个整合和解释说明
<>强创建iOS原生文件强>
创建iOS原生文件来实现交互,首先要创建个原生文件。命名为FlutterIOSDevicePlugin。创建文件
<人物> 图>
创建一个命名为FlutterIOSDevicePlugin。h和命名为FlutterIOSDevicePlugin。米文件,说明一下:FlutterIOSDevicePlugin。h文件选择图片里面模的头文件块创建即可。
创建之后,一起看一下FlutterIOSDevicePlugin.h里面的代码
的ifndef FlutterIOSDevicePlugin_h #定义FlutterIOSDevicePlugin_h # import & lt;颤振/Flutter.h> @ interface FlutterIOSDevicePlugin: NSObject+ (void) registerWithRegistrar:(NSObject *)注册flutterViewController: (flutterViewController *)控制器; - (instancetype) newInstance:(NSObject *)注册flutterViewController: (flutterViewController *)控制器; @end # endif/* FlutterIOSDevicePlugin_h */
这个不需要过多说明了看一下FlutterIOSDevicePlugin.m的代码
# import & lt;基?Foundation.h> #进口“FlutterIOSDevicePlugin.h” @ interface FlutterIOSDevicePlugin () { NSObject* _registrar; FlutterViewController * _controller; } @end 静态NSString * const CHANNEL_NAME=@“flutter_ios_device”; 静态NSString * const METHOD_CHANGE_ORIENTATION=@“change_screen_orientation”; 静态NSString * const ORIENTATION_PORTRAIT_UP=@“portraitUp”; 静态NSString * const ORIENTATION_PORTRAIT_DOWN=@“portraitDown”; 静态NSString * const ORIENTATION_LANDSCAPE_LEFT=@“landscapeLeft”; 静态NSString * const ORIENTATION_LANDSCAPE_RIGHT=@“landscapeRight”; @ implementation FlutterIOSDevicePlugin + (void) registerWithRegistrar:(NSObject *)注册{ FlutterMethodChannel *=[FlutterMethodChannel频道 methodChannelWithName: CHANNEL_NAME binaryMessenger:[注册信使]]; FlutterIOSDevicePlugin *实例=[[FlutterIOSDevicePlugin alloc] newInstance:注册flutterViewController: nil); (注册addMethodCallDelegate:实例通道:通道); } + (void) registerWithRegistrar:(NSObject *)注册flutterViewController:控制器(flutterViewController *) { FlutterMethodChannel *=[FlutterMethodChannel频道 methodChannelWithName: CHANNEL_NAME binaryMessenger:[注册信使]]; FlutterIOSDevicePlugin *实例=[[FlutterIOSDevicePlugin alloc] newInstance:注册flutterViewController:控制器); (注册addMethodCallDelegate:实例通道:通道); } - (instancetype) newInstance:(NSObject *)注册flutterViewController:控制器(flutterViewController *) { _registrar=注册; _controller=控制器; 回归自我; } - (void) handleMethodCall: (FlutterMethodCall *)调用结果:结果(FlutterResult) { 如果([METHOD_CHANGE_ORIENTATION isEqualToString: call.method]) { NSArray *参数=call.arguments; NSString *取向[0]=参数; NSNumber *指数=[NSNumber numberWithInt:(电话。参数[0]intValue]]; NSInteger iOSOrientation; 如果([取向isEqualToString: ORIENTATION_LANDSCAPE_LEFT]) { iOSOrientation=UIDeviceOrientationLandscapeLeft; }else if([取向isEqualToString: ORIENTATION_LANDSCAPE_RIGHT]) { iOSOrientation=UIDeviceOrientationLandscapeRight; }else if([取向isEqualToString: ORIENTATION_PORTRAIT_DOWN]) { iOSOrientation=UIDeviceOrientationPortraitUpsideDown; 其他}{ iOSOrientation=UIDeviceOrientationPortrait; } [[UIDevice currentDevice] setValue: @ (iOSOrientation) forKey: @“取向”);//[[NSNotificationCenter defaultCenter] postNotificationName: @“FlutterIOSDevicePlugin”对象:零用户信息:@ {@“orientationMask”:指数});//(attemptRotationToDeviceOrientation ui); 结果(nil); 其他}{ 结果(FlutterMethodNotImplemented); } } @end 颤振控制屏幕旋转的实现