屏幕旋转和弹出框

iOS中控制屏幕旋转相关方法

shouldAutorotate:是否支持屏幕旋转

{
    //返回值为YES支持屏幕旋转,返回值为NO不支持屏幕旋转,默认值为YES。
    return YES;
}```

supportedInterfaceOrientations:设置当前界面所支持的屏幕方向
```- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    //支持所有的屏幕方向
    return UIInterfaceOrientationMaskAll;
    //支持向左或向右
    //return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}```

layoutSubviews:重写父类的layoutSubviews,当屏幕旋转时会执行此方法,一般在此方法中对当前的子视图重新布局
```- (void)layoutSubviews
{
    [super layoutSubviews];//调用父类的方法
    //获取当前屏幕的方向状态, [UIApplication sharedApplication]获得当前的应用程序
    NSInteger orientation = [UIApplication sharedApplication].statusBarOrientation;
    switch (orientation) {
        //屏幕左转时的重新布局
        case UIInterfaceOrientationLandscapeLeft:
            {
              ......
            }
            break;
        //屏幕右转时的重新布局
        case UIInterfaceOrientationLandscapeRight:
            {
              ......
            }
            break;
        //屏幕转回来后的重新布局  
        case UIInterfaceOrientationPortrait:
            {
              ......
            }
            break;
        default:
            break;
    }
}```

#警示框
iOS8.0之前
警示框:
```//创建一个警示框并初始化
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标题" message:@"内容" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", @"再次确定", nil];
//显示alertView
[alertView show];

alertView:clickedButtonAtIndex:警示框的代理方法

{
    NSLog(@"buttonIndex = %ld",buttonIndex);//输出点击按钮的标记index,根据添加顺序从0开始一次增加
}```

屏幕下方弹框:
```//初始化sheet
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"sheet" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其余", nil];
//显示
[sheet showInView:sheet];```

actionSheet:clickedButtonAtIndex:屏幕下方弹框的代理方法
```- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"buttonIndex = %ld",buttonIndex);//输出点击按钮的标记index,根据添加顺序从0开始一次增加
}```

iOS8.0之后
UIAlertController:提示框控制器 这是iOS8之后出得新类,用于替换原来的UIAlertView 和 UIActionSheet
*  title:提示框按钮的标题
*  style:按钮的样式
*  handler:点击该按钮就会执行的语句(block)

//初始化一个UIAlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示框" message:@"内容" preferredStyle:UIAlertControllerStyleAlert];
//为提示框添加点击按钮alertAction
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了取消按钮");
}];
//将创建好的action添加到提示框视图控制器上
[alertController addAction:cancelAction];
//再添加一个确定按钮
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了确定按钮");
}];
[alertController addAction:sureAction];```

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 大纲 一、UIAlertView项目:UIAlertView0314步骤: 1.创建AlertView 2.实现可...
    AsaGuo阅读 3,280评论 0 0
  • 自从iOS9出来之后,需要使用UIAlertController来弹出弹框,不在提倡使用UIAlertView了,...
    南京杨小兵阅读 3,593评论 1 0
  • { 11、核心动画 需要签协议,但是系统帮签好 一、CABasicAnimation 1、创建基础动画对象 CAB...
    CYC666阅读 5,486评论 2 4
  • 怪物背景: 事发当天我正戴着耳机悠闲的听着普通DISCO,有节奏的敲着代码。忽然,旁边办公桌发出一声巨响~ 带着几...
    嘞似彩笔阅读 4,400评论 2 1
  • 代码下载地址一代码下载地址二 前言 在iOS应用程序中,我们经常看到各种各样的弹出框,比如地理信息、相册、相机、权...
    酒茶白开水阅读 25,882评论 11 28