学习iOS技术程序猿和程序媛程序员

屏幕旋转和弹出框

2015-11-30  本文已影响830人  ly渐行渐远

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];```

上一篇下一篇

猜你喜欢

热点阅读