美文网首页
屏幕旋转

屏幕旋转

作者: ThEAll | 来源:发表于2015-12-03 17:16 被阅读89次

import "AppDelegate.h"

import "ViewController.h"

@interface AppDelegate ()

@end
@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    ViewController *viewControiller = [[ViewController alloc]init];
    [self.window setRootViewController:viewControiller];

    return YES;
    }


import "ViewController.h"

import "LoginView.h"

@interface ViewController ()

@end

@implementation ViewController

// 是否支持屏幕旋转(默认的是YES)
-(BOOL)shouldAutorotate{
// 如果返回值为YES,支持旋转,返回值为NO,不支持旋转

//  作业:登录界面旋转后重新布局
return YES;

}

// 设置当前界面(viewControler自带的view)所支持的屏幕的方向,系统默认支持竖直,横向向左,横向向右
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
// 支持所有的屏幕方向
return UIInterfaceOrientationMaskAll;
}

-(void)loadView{
[super loadView];

LoginView *loginView = [[LoginView alloc]init];
self.view = loginView;  

}

import "LoginView.h"

@interface LoginView ()

@property (nonatomic ,retain)UITextField *textField;

@end

@implementation LoginView
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self textField];
}return self;
}
-(UITextField*)textField{
if (!_textField) {

    _textField = [[UITextField alloc]initWithFrame:CGRectMake(80, 100, 100, 40)];

    _textField.backgroundColor = [UIColor redColor];
    [self addSubview:_textField];
}return _textField;

}

// 重写的父类的layoutSubviews,当屏幕旋转的时候,会执行此方法。一般在此方法中对当前view的子视图进行重新布局
-(void)layoutSubviews{
[super layoutSubviews];
// 获取当前屏幕的方向状态
// [UIApplication sharedApplication]:获得当前的应用程序
NSInteger orientation = [UIApplication sharedApplication].statusBarOrientation;
switch (orientation) {

    //  当屏幕方向为水平向左时,我们要将输入框水平居中
    case UIInterfaceOrientationLandscapeLeft:
    {
        CGRect frame = self.textField.frame;
        frame.origin.x = 0;
        self.textField.frame = frame;
    }
        break;
       
        case UIInterfaceOrientationLandscapeRight:
        //  如果是多行执行语句的时候,需要用花括号将他们括起来
    {
        CGRect frame = self.textField.frame;
        frame.origin.x = (CGRectGetWidth(self.frame) - CGRectGetWidth(self.textField.frame))/2;
        self.textField.frame = frame;
    }
        break;
      
    case UIInterfaceOrientationPortrait:{
        CGRect frame = self.textField.frame;
        frame.origin.x = 80;
        self.textField.frame = frame;
    }
        break;
     
    default:
        break;
}

}

相关文章

  • iOS 屏幕旋转

    屏幕旋转 认知 期望达到的目的 如何让App支持屏幕旋转 如何让App屏幕旋转 如何保证屏幕旋转后布局不会乱 总结...

  • 屏幕旋转

    屏幕旋转 推荐文档 了解UIWindow——UIWindow实践 iOS屏幕旋转问题总结 IOS:屏幕旋转与变换 ...

  • 屏幕旋转

    UIDevice.current.setValue(UIInterfaceOrientation.landscap...

  • 屏幕旋转

    import "AppDelegate.h" import "ViewController.h" @interfa...

  • 屏幕旋转

    在做工程的时候碰到了屏幕旋转的问题,如今已经解决.为大家分享一下 屏幕旋转机制流程 (1)加速计检测到方向变化,发...

  • 屏幕旋转

    每个视图控制器都控制着自己的旋转方向,如果需要新的旋转权限需要模态出新的视图控制器(如navigation tab...

  • 屏幕旋转

    当activity设置默认属性的时候:竖屏和横屏旋转可以通过监听onConfigurationChanged来判断...

  • 屏幕旋转

    在创建的vc中 //指定能够支持的orientation有哪些 -(UIInterfaceOrientationM...

  • 屏幕旋转

    本文涉及到的转屏是咱们的app的某个页面设置横竖屏的切换 必须先在appdelegate中实现下面的方法-(UII...

  • 屏幕旋转

    第一步 在 AppDelegate.h 里增加一个属性 用来区分哪个界面可以横屏 哪个界面不可以 第二步 在 Ap...

网友评论

      本文标题:屏幕旋转

      本文链接:https://www.haomeiwen.com/subject/uwrshttx.html